@praeviso/code-env-switch 0.1.2 → 0.1.4
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/bin/statusline/debug.js +42 -0
- package/bin/statusline/format.js +60 -0
- package/bin/statusline/git.js +96 -0
- package/bin/statusline/index.js +73 -400
- package/bin/statusline/input.js +249 -0
- package/bin/statusline/style.js +22 -0
- package/bin/statusline/types.js +2 -0
- package/bin/statusline/usage.js +123 -0
- package/bin/statusline/utils.js +35 -0
- package/bin/usage/index.js +133 -10
- package/package.json +1 -1
- package/src/statusline/debug.ts +40 -0
- package/src/statusline/format.ts +68 -0
- package/src/statusline/git.ts +82 -0
- package/src/statusline/index.ts +93 -470
- package/src/statusline/input.ts +300 -0
- package/src/statusline/style.ts +19 -0
- package/src/statusline/types.ts +105 -0
- package/src/statusline/usage.ts +175 -0
- package/src/statusline/utils.ts +27 -0
- package/src/usage/index.ts +156 -10
package/src/statusline/index.ts
CHANGED
|
@@ -1,478 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Statusline builder
|
|
3
3
|
*/
|
|
4
|
-
import * as fs from "fs";
|
|
5
|
-
import * as path from "path";
|
|
6
|
-
import { spawnSync } from "child_process";
|
|
7
4
|
import type { Config, StatuslineArgs } from "../types";
|
|
8
|
-
import { DEFAULT_PROFILE_TYPES } from "../constants";
|
|
9
5
|
import { normalizeType, inferProfileType, getProfileDisplayName } from "../profile/type";
|
|
6
|
+
import { syncUsageFromStatuslineInput } from "../usage";
|
|
7
|
+
import { appendStatuslineDebug } from "./debug";
|
|
10
8
|
import {
|
|
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
|
-
profile?: StatuslineInputProfile;
|
|
39
|
-
model?: string | StatuslineInputModel;
|
|
40
|
-
model_provider?: string;
|
|
41
|
-
usage?: StatuslineInputUsage;
|
|
42
|
-
token_usage?: StatuslineInputUsage | number | Record<string, unknown>;
|
|
43
|
-
git_branch?: string;
|
|
44
|
-
task_running?: boolean;
|
|
45
|
-
review_mode?: boolean;
|
|
46
|
-
context_window_percent?: number;
|
|
47
|
-
context_window_used_tokens?: number;
|
|
48
|
-
workspace?: {
|
|
49
|
-
current_dir?: string;
|
|
50
|
-
project_dir?: string;
|
|
51
|
-
};
|
|
52
|
-
cost?: Record<string, unknown>;
|
|
53
|
-
version?: string;
|
|
54
|
-
output_style?: { name?: string };
|
|
55
|
-
session_id?: string;
|
|
56
|
-
transcript_path?: string;
|
|
57
|
-
hook_event_name?: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
interface StatuslineUsage {
|
|
61
|
-
todayTokens: number | null;
|
|
62
|
-
totalTokens: number | null;
|
|
63
|
-
inputTokens: number | null;
|
|
64
|
-
outputTokens: number | null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
interface GitStatus {
|
|
68
|
-
branch: string | null;
|
|
69
|
-
ahead: number;
|
|
70
|
-
behind: number;
|
|
71
|
-
staged: number;
|
|
72
|
-
unstaged: number;
|
|
73
|
-
untracked: number;
|
|
74
|
-
conflicted: number;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const COLOR_ENABLED = !process.env.NO_COLOR && process.env.TERM !== "dumb";
|
|
78
|
-
const ANSI_RESET = "\x1b[0m";
|
|
79
|
-
const ICON_GIT = "⎇";
|
|
80
|
-
const ICON_PROFILE = "👤";
|
|
81
|
-
const ICON_MODEL = "⚙";
|
|
82
|
-
const ICON_USAGE = "⚡";
|
|
83
|
-
const ICON_CONTEXT = "🧠";
|
|
84
|
-
const ICON_REVIEW = "📝";
|
|
85
|
-
const ICON_CWD = "📁";
|
|
86
|
-
|
|
87
|
-
function colorize(text: string, colorCode: string): string {
|
|
88
|
-
if (!COLOR_ENABLED) return text;
|
|
89
|
-
return `\x1b[${colorCode}m${text}${ANSI_RESET}`;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function dim(text: string): string {
|
|
93
|
-
return colorize(text, "2");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function getCwdSegment(cwd: string): string | null {
|
|
97
|
-
if (!cwd) return null;
|
|
98
|
-
const base = path.basename(cwd) || cwd;
|
|
99
|
-
const segment = `${ICON_CWD} ${base}`;
|
|
100
|
-
return dim(segment);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export interface StatuslineJson {
|
|
104
|
-
cwd: string;
|
|
105
|
-
type: string | null;
|
|
106
|
-
profile: { key: string | null; name: string | null };
|
|
107
|
-
model: string | null;
|
|
108
|
-
usage: StatuslineUsage | null;
|
|
109
|
-
git: GitStatus | null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface StatuslineResult {
|
|
113
|
-
text: string;
|
|
114
|
-
json: StatuslineJson;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
118
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function readStdinJson(): StatuslineInput | null {
|
|
122
|
-
if (process.stdin.isTTY) return null;
|
|
123
|
-
try {
|
|
124
|
-
const raw = fs.readFileSync(0, "utf8");
|
|
125
|
-
const trimmed = raw.trim();
|
|
126
|
-
if (!trimmed) return null;
|
|
127
|
-
const parsed = JSON.parse(trimmed);
|
|
128
|
-
if (!isRecord(parsed)) return null;
|
|
129
|
-
return parsed as StatuslineInput;
|
|
130
|
-
} catch {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function firstNonEmpty(...values: Array<string | null | undefined>): string | null {
|
|
136
|
-
for (const value of values) {
|
|
137
|
-
if (value === null || value === undefined) continue;
|
|
138
|
-
const text = String(value).trim();
|
|
139
|
-
if (text) return text;
|
|
140
|
-
}
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function coerceNumber(value: unknown): number | null {
|
|
145
|
-
if (value === null || value === undefined || value === "") return null;
|
|
146
|
-
const num = Number(value);
|
|
147
|
-
if (!Number.isFinite(num)) return null;
|
|
148
|
-
return num;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function firstNumber(...values: Array<unknown>): number | null {
|
|
152
|
-
for (const value of values) {
|
|
153
|
-
const num = coerceNumber(value);
|
|
154
|
-
if (num !== null) return num;
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function normalizeTypeValue(value: string | null): string | null {
|
|
160
|
-
if (!value) return null;
|
|
161
|
-
const normalized = normalizeType(value);
|
|
162
|
-
if (normalized) return normalized;
|
|
163
|
-
const trimmed = String(value).trim();
|
|
164
|
-
return trimmed ? trimmed : null;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function detectTypeFromEnv(): string | null {
|
|
168
|
-
const matches = DEFAULT_PROFILE_TYPES.filter((type) => {
|
|
169
|
-
const suffix = type.toUpperCase();
|
|
170
|
-
return (
|
|
171
|
-
process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] ||
|
|
172
|
-
process.env[`CODE_ENV_PROFILE_NAME_${suffix}`]
|
|
173
|
-
);
|
|
174
|
-
});
|
|
175
|
-
if (matches.length === 1) return matches[0];
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function resolveEnvProfile(type: string | null): { key: string | null; name: string | null } {
|
|
180
|
-
const genericKey = process.env.CODE_ENV_PROFILE_KEY || null;
|
|
181
|
-
const genericName = process.env.CODE_ENV_PROFILE_NAME || null;
|
|
182
|
-
if (!type) {
|
|
183
|
-
return { key: genericKey, name: genericName };
|
|
184
|
-
}
|
|
185
|
-
const suffix = type.toUpperCase();
|
|
186
|
-
const key = process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] || genericKey;
|
|
187
|
-
const name = process.env[`CODE_ENV_PROFILE_NAME_${suffix}`] || genericName;
|
|
188
|
-
return { key: key || null, name: name || null };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function getModelFromInput(input: StatuslineInput | null): string | null {
|
|
192
|
-
if (!input) return null;
|
|
193
|
-
const raw = input.model;
|
|
194
|
-
if (!raw) return null;
|
|
195
|
-
if (typeof raw === "string") return raw;
|
|
196
|
-
if (isRecord(raw)) {
|
|
197
|
-
const displayName = raw.displayName || raw.display_name;
|
|
198
|
-
if (displayName) return String(displayName);
|
|
199
|
-
if (raw.id) return String(raw.id);
|
|
200
|
-
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function getModelProviderFromInput(input: StatuslineInput | null): string | null {
|
|
205
|
-
if (!input || !input.model_provider) return null;
|
|
206
|
-
const provider = String(input.model_provider).trim();
|
|
207
|
-
return provider ? provider : null;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function getInputProfile(input: StatuslineInput | null): StatuslineInputProfile | null {
|
|
211
|
-
if (!input || !isRecord(input.profile)) return null;
|
|
212
|
-
return input.profile as StatuslineInputProfile;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function getInputUsage(input: StatuslineInput | null): StatuslineInputUsage | null {
|
|
216
|
-
if (!input) return null;
|
|
217
|
-
if (isRecord(input.usage)) {
|
|
218
|
-
return input.usage as StatuslineInputUsage;
|
|
219
|
-
}
|
|
220
|
-
const tokenUsage = input.token_usage;
|
|
221
|
-
if (tokenUsage === null || tokenUsage === undefined) return null;
|
|
222
|
-
if (typeof tokenUsage === "number") {
|
|
223
|
-
return {
|
|
224
|
-
todayTokens: null,
|
|
225
|
-
totalTokens: coerceNumber(tokenUsage),
|
|
226
|
-
inputTokens: null,
|
|
227
|
-
outputTokens: null,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
if (isRecord(tokenUsage)) {
|
|
231
|
-
const record = tokenUsage as Record<string, unknown>;
|
|
232
|
-
return {
|
|
233
|
-
todayTokens:
|
|
234
|
-
firstNumber(
|
|
235
|
-
record.todayTokens,
|
|
236
|
-
record.today,
|
|
237
|
-
record.today_tokens,
|
|
238
|
-
record.daily,
|
|
239
|
-
record.daily_tokens
|
|
240
|
-
) ?? null,
|
|
241
|
-
totalTokens:
|
|
242
|
-
firstNumber(
|
|
243
|
-
record.totalTokens,
|
|
244
|
-
record.total,
|
|
245
|
-
record.total_tokens
|
|
246
|
-
) ?? null,
|
|
247
|
-
inputTokens:
|
|
248
|
-
firstNumber(
|
|
249
|
-
record.inputTokens,
|
|
250
|
-
record.input,
|
|
251
|
-
record.input_tokens
|
|
252
|
-
) ?? null,
|
|
253
|
-
outputTokens:
|
|
254
|
-
firstNumber(
|
|
255
|
-
record.outputTokens,
|
|
256
|
-
record.output,
|
|
257
|
-
record.output_tokens
|
|
258
|
-
) ?? null,
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
return null;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function getContextUsedTokens(input: StatuslineInput | null): number | null {
|
|
265
|
-
if (!input) return null;
|
|
266
|
-
return coerceNumber(input.context_window_used_tokens);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function getContextLeftPercent(
|
|
270
|
-
input: StatuslineInput | null,
|
|
271
|
-
type: string | null
|
|
272
|
-
): number | null {
|
|
273
|
-
if (!input) return null;
|
|
274
|
-
const raw = coerceNumber(input.context_window_percent);
|
|
275
|
-
if (raw === null || raw < 0) return null;
|
|
276
|
-
const percent = raw <= 1 ? raw * 100 : raw;
|
|
277
|
-
if (percent > 100) return null;
|
|
278
|
-
const usedTokens = getContextUsedTokens(input);
|
|
279
|
-
const normalizedType = normalizeTypeValue(type);
|
|
280
|
-
// Prefer treating the percent as "remaining" for codex/claude and when usage is absent.
|
|
281
|
-
const preferRemaining =
|
|
282
|
-
normalizedType === "codex" ||
|
|
283
|
-
normalizedType === "claude" ||
|
|
284
|
-
usedTokens === null ||
|
|
285
|
-
(usedTokens <= 0 && percent >= 99);
|
|
286
|
-
const left = preferRemaining ? percent : 100 - percent;
|
|
287
|
-
return Math.max(0, Math.min(100, left));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function getWorkspaceDir(input: StatuslineInput | null): string | null {
|
|
291
|
-
if (!input || !isRecord(input.workspace)) return null;
|
|
292
|
-
const currentDir = input.workspace.current_dir;
|
|
293
|
-
if (currentDir) {
|
|
294
|
-
const trimmed = String(currentDir).trim();
|
|
295
|
-
if (trimmed) return trimmed;
|
|
296
|
-
}
|
|
297
|
-
const projectDir = input.workspace.project_dir;
|
|
298
|
-
if (!projectDir) return null;
|
|
299
|
-
const trimmed = String(projectDir).trim();
|
|
300
|
-
return trimmed ? trimmed : null;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function getGitStatusFromInput(
|
|
304
|
-
input: StatuslineInput | null
|
|
305
|
-
): GitStatus | null {
|
|
306
|
-
if (!input || !input.git_branch) return null;
|
|
307
|
-
const branch = String(input.git_branch).trim();
|
|
308
|
-
if (!branch) return null;
|
|
309
|
-
return {
|
|
310
|
-
branch,
|
|
311
|
-
ahead: 0,
|
|
312
|
-
behind: 0,
|
|
313
|
-
staged: 0,
|
|
314
|
-
unstaged: 0,
|
|
315
|
-
untracked: 0,
|
|
316
|
-
conflicted: 0,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function getGitStatus(cwd: string): GitStatus | null {
|
|
321
|
-
if (!cwd) return null;
|
|
322
|
-
const result = spawnSync("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
|
|
323
|
-
encoding: "utf8",
|
|
324
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
325
|
-
});
|
|
326
|
-
if (result.status !== 0 || !result.stdout) return null;
|
|
327
|
-
const status: GitStatus = {
|
|
328
|
-
branch: null,
|
|
329
|
-
ahead: 0,
|
|
330
|
-
behind: 0,
|
|
331
|
-
staged: 0,
|
|
332
|
-
unstaged: 0,
|
|
333
|
-
untracked: 0,
|
|
334
|
-
conflicted: 0,
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const lines = result.stdout.split(/\r?\n/);
|
|
338
|
-
for (const line of lines) {
|
|
339
|
-
if (!line) continue;
|
|
340
|
-
if (line.startsWith("# branch.head ")) {
|
|
341
|
-
status.branch = line.slice("# branch.head ".length).trim();
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
if (line.startsWith("# branch.ab ")) {
|
|
345
|
-
const parts = line
|
|
346
|
-
.slice("# branch.ab ".length)
|
|
347
|
-
.trim()
|
|
348
|
-
.split(/\s+/);
|
|
349
|
-
for (const part of parts) {
|
|
350
|
-
if (part.startsWith("+")) status.ahead = Number(part.slice(1)) || 0;
|
|
351
|
-
if (part.startsWith("-")) status.behind = Number(part.slice(1)) || 0;
|
|
352
|
-
}
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
if (line.startsWith("? ")) {
|
|
356
|
-
status.untracked += 1;
|
|
357
|
-
continue;
|
|
358
|
-
}
|
|
359
|
-
if (line.startsWith("u ")) {
|
|
360
|
-
status.conflicted += 1;
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
if (line.startsWith("1 ") || line.startsWith("2 ")) {
|
|
364
|
-
const parts = line.split(/\s+/);
|
|
365
|
-
const xy = parts[1] || "";
|
|
366
|
-
const staged = xy[0];
|
|
367
|
-
const unstaged = xy[1];
|
|
368
|
-
if (staged && staged !== ".") status.staged += 1;
|
|
369
|
-
if (unstaged && unstaged !== ".") status.unstaged += 1;
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (!status.branch) {
|
|
375
|
-
status.branch = "HEAD";
|
|
376
|
-
}
|
|
377
|
-
return status;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function formatGitSegment(status: GitStatus | null): string | null {
|
|
381
|
-
if (!status || !status.branch) return null;
|
|
382
|
-
const meta: string[] = [];
|
|
383
|
-
const dirtyCount = status.staged + status.unstaged + status.untracked;
|
|
384
|
-
if (status.ahead > 0) meta.push(`↑${status.ahead}`);
|
|
385
|
-
if (status.behind > 0) meta.push(`↓${status.behind}`);
|
|
386
|
-
if (status.conflicted > 0) meta.push(`✖${status.conflicted}`);
|
|
387
|
-
if (dirtyCount > 0) meta.push(`+${dirtyCount}`);
|
|
388
|
-
const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
|
|
389
|
-
const text = `${ICON_GIT} ${status.branch}${suffix}`;
|
|
390
|
-
const hasConflicts = status.conflicted > 0;
|
|
391
|
-
const isDirty = dirtyCount > 0;
|
|
392
|
-
if (hasConflicts) return colorize(text, "31");
|
|
393
|
-
if (isDirty) return colorize(text, "33");
|
|
394
|
-
if (status.ahead > 0 || status.behind > 0) return colorize(text, "36");
|
|
395
|
-
return colorize(text, "32");
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function resolveUsageFromRecords(
|
|
399
|
-
config: Config,
|
|
400
|
-
configPath: string | null,
|
|
401
|
-
type: string | null,
|
|
402
|
-
profileKey: string | null,
|
|
403
|
-
profileName: string | null,
|
|
404
|
-
syncUsage: boolean
|
|
405
|
-
): StatuslineUsage | null {
|
|
406
|
-
try {
|
|
407
|
-
const normalized = normalizeType(type || "");
|
|
408
|
-
if (!normalized || (!profileKey && !profileName)) return null;
|
|
409
|
-
const totals = readUsageTotalsIndex(config, configPath, syncUsage);
|
|
410
|
-
if (!totals) return null;
|
|
411
|
-
const usage = resolveUsageTotalsForProfile(
|
|
412
|
-
totals,
|
|
413
|
-
normalized,
|
|
414
|
-
profileKey,
|
|
415
|
-
profileName
|
|
416
|
-
);
|
|
417
|
-
if (!usage) return null;
|
|
418
|
-
return {
|
|
419
|
-
todayTokens: usage.today,
|
|
420
|
-
totalTokens: usage.total,
|
|
421
|
-
inputTokens: null,
|
|
422
|
-
outputTokens: null,
|
|
423
|
-
};
|
|
424
|
-
} catch {
|
|
425
|
-
return null;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
function formatUsageSegment(usage: StatuslineUsage | null): string | null {
|
|
430
|
-
if (!usage) return null;
|
|
431
|
-
const today =
|
|
432
|
-
usage.todayTokens ??
|
|
433
|
-
(usage.inputTokens !== null || usage.outputTokens !== null
|
|
434
|
-
? (usage.inputTokens || 0) + (usage.outputTokens || 0)
|
|
435
|
-
: usage.totalTokens);
|
|
436
|
-
if (today === null) return null;
|
|
437
|
-
const text = `Today ${formatTokenCount(today)}`;
|
|
438
|
-
return colorize(`${ICON_USAGE} ${text}`, "33");
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function formatModelSegment(
|
|
442
|
-
model: string | null,
|
|
443
|
-
provider: string | null
|
|
444
|
-
): string | null {
|
|
445
|
-
if (!model) return null;
|
|
446
|
-
const providerLabel = provider ? `${provider}:${model}` : model;
|
|
447
|
-
return colorize(`${ICON_MODEL} ${providerLabel}`, "35");
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function formatProfileSegment(
|
|
451
|
-
type: string | null,
|
|
452
|
-
profileKey: string | null,
|
|
453
|
-
profileName: string | null
|
|
454
|
-
): string | null {
|
|
455
|
-
const name = profileName || profileKey;
|
|
456
|
-
if (!name) return null;
|
|
457
|
-
const label = type ? `${type}:${name}` : name;
|
|
458
|
-
return colorize(`${ICON_PROFILE} ${label}`, "37");
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
function formatContextSegment(contextLeft: number | null): string | null {
|
|
462
|
-
if (contextLeft === null) return null;
|
|
463
|
-
const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
|
|
464
|
-
return colorize(`${ICON_CONTEXT} ${left}% left`, "36");
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function formatContextUsedSegment(usedTokens: number | null): string | null {
|
|
468
|
-
if (usedTokens === null) return null;
|
|
469
|
-
return colorize(`${ICON_CONTEXT} ${formatTokenCount(usedTokens)} used`, "36");
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
function formatModeSegment(reviewMode: boolean): string | null {
|
|
473
|
-
if (!reviewMode) return null;
|
|
474
|
-
return colorize(`${ICON_REVIEW} review`, "34");
|
|
475
|
-
}
|
|
9
|
+
formatContextSegment,
|
|
10
|
+
formatContextUsedSegment,
|
|
11
|
+
formatModeSegment,
|
|
12
|
+
formatModelSegment,
|
|
13
|
+
formatProfileSegment,
|
|
14
|
+
formatUsageSegment,
|
|
15
|
+
getCwdSegment,
|
|
16
|
+
} from "./format";
|
|
17
|
+
import { formatGitSegment, getGitStatus } from "./git";
|
|
18
|
+
import {
|
|
19
|
+
detectTypeFromEnv,
|
|
20
|
+
getContextLeftPercent,
|
|
21
|
+
getContextUsedTokens,
|
|
22
|
+
getGitStatusFromInput,
|
|
23
|
+
getInputProfile,
|
|
24
|
+
getInputUsage,
|
|
25
|
+
getModelFromInput,
|
|
26
|
+
getModelProviderFromInput,
|
|
27
|
+
getSessionId,
|
|
28
|
+
getWorkspaceDir,
|
|
29
|
+
normalizeTypeValue,
|
|
30
|
+
readStdinJson,
|
|
31
|
+
resolveEnvProfile,
|
|
32
|
+
} from "./input";
|
|
33
|
+
import { getUsageTotalsFromInput, normalizeInputUsage, resolveUsageFromRecords } from "./usage";
|
|
34
|
+
import { firstNonEmpty, firstNumber } from "./utils";
|
|
35
|
+
import type { StatuslineResult, StatuslineUsage } from "./types";
|
|
476
36
|
|
|
477
37
|
export function buildStatuslineResult(
|
|
478
38
|
args: StatuslineArgs,
|
|
@@ -496,7 +56,7 @@ export function buildStatuslineResult(
|
|
|
496
56
|
let type = normalizeTypeValue(typeCandidate);
|
|
497
57
|
const envProfile = resolveEnvProfile(type);
|
|
498
58
|
|
|
499
|
-
|
|
59
|
+
const profileKey = firstNonEmpty(
|
|
500
60
|
args.profileKey,
|
|
501
61
|
envProfile.key,
|
|
502
62
|
inputProfile ? inputProfile.key : null
|
|
@@ -530,6 +90,50 @@ export function buildStatuslineResult(
|
|
|
530
90
|
process.cwd()
|
|
531
91
|
)!;
|
|
532
92
|
|
|
93
|
+
const sessionId = getSessionId(stdinInput);
|
|
94
|
+
const stdinUsageTotals = getUsageTotalsFromInput(stdinInput);
|
|
95
|
+
const usageType = normalizeType(type || "");
|
|
96
|
+
appendStatuslineDebug(configPath, {
|
|
97
|
+
timestamp: new Date().toISOString(),
|
|
98
|
+
typeCandidate,
|
|
99
|
+
resolvedType: type,
|
|
100
|
+
usageType,
|
|
101
|
+
profile: { key: profileKey, name: profileName },
|
|
102
|
+
sessionId,
|
|
103
|
+
stdinUsageTotals,
|
|
104
|
+
cwd,
|
|
105
|
+
args: {
|
|
106
|
+
type: args.type,
|
|
107
|
+
profileKey: args.profileKey,
|
|
108
|
+
profileName: args.profileName,
|
|
109
|
+
usageToday: args.usageToday,
|
|
110
|
+
usageTotal: args.usageTotal,
|
|
111
|
+
usageInput: args.usageInput,
|
|
112
|
+
usageOutput: args.usageOutput,
|
|
113
|
+
syncUsage: args.syncUsage,
|
|
114
|
+
},
|
|
115
|
+
env: {
|
|
116
|
+
CODE_ENV_TYPE: process.env.CODE_ENV_TYPE,
|
|
117
|
+
CODE_ENV_PROFILE_KEY: process.env.CODE_ENV_PROFILE_KEY,
|
|
118
|
+
CODE_ENV_PROFILE_NAME: process.env.CODE_ENV_PROFILE_NAME,
|
|
119
|
+
CODE_ENV_CWD: process.env.CODE_ENV_CWD,
|
|
120
|
+
CODE_ENV_STATUSLINE: process.env.CODE_ENV_STATUSLINE,
|
|
121
|
+
},
|
|
122
|
+
input: stdinInput,
|
|
123
|
+
});
|
|
124
|
+
if (args.syncUsage && sessionId && stdinUsageTotals) {
|
|
125
|
+
syncUsageFromStatuslineInput(
|
|
126
|
+
config,
|
|
127
|
+
configPath,
|
|
128
|
+
usageType,
|
|
129
|
+
profileKey,
|
|
130
|
+
profileName,
|
|
131
|
+
sessionId,
|
|
132
|
+
stdinUsageTotals,
|
|
133
|
+
cwd
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
533
137
|
const model = firstNonEmpty(
|
|
534
138
|
args.model,
|
|
535
139
|
process.env.CODE_ENV_MODEL,
|
|
@@ -565,7 +169,12 @@ export function buildStatuslineResult(
|
|
|
565
169
|
usage.inputTokens !== null ||
|
|
566
170
|
usage.outputTokens !== null;
|
|
567
171
|
|
|
172
|
+
const stdinUsage = normalizeInputUsage(getInputUsage(stdinInput));
|
|
173
|
+
|
|
568
174
|
let finalUsage: StatuslineUsage | null = hasExplicitUsage ? usage : null;
|
|
175
|
+
if (!finalUsage) {
|
|
176
|
+
finalUsage = stdinUsage;
|
|
177
|
+
}
|
|
569
178
|
if (!finalUsage) {
|
|
570
179
|
finalUsage = resolveUsageFromRecords(
|
|
571
180
|
config,
|
|
@@ -624,3 +233,17 @@ export function buildStatuslineResult(
|
|
|
624
233
|
},
|
|
625
234
|
};
|
|
626
235
|
}
|
|
236
|
+
|
|
237
|
+
export type {
|
|
238
|
+
GitStatus,
|
|
239
|
+
StatuslineInput,
|
|
240
|
+
StatuslineInputContextWindow,
|
|
241
|
+
StatuslineInputContextWindowUsage,
|
|
242
|
+
StatuslineInputModel,
|
|
243
|
+
StatuslineInputProfile,
|
|
244
|
+
StatuslineInputUsage,
|
|
245
|
+
StatuslineJson,
|
|
246
|
+
StatuslineResult,
|
|
247
|
+
StatuslineUsage,
|
|
248
|
+
StatuslineUsageTotals,
|
|
249
|
+
} from "./types";
|