@praeviso/code-env-switch 0.1.3 → 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 +67 -556
- 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/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 +72 -743
- 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
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readStdinJson = readStdinJson;
|
|
4
|
+
exports.normalizeTypeValue = normalizeTypeValue;
|
|
5
|
+
exports.detectTypeFromEnv = detectTypeFromEnv;
|
|
6
|
+
exports.resolveEnvProfile = resolveEnvProfile;
|
|
7
|
+
exports.getModelFromInput = getModelFromInput;
|
|
8
|
+
exports.getModelProviderFromInput = getModelProviderFromInput;
|
|
9
|
+
exports.getInputProfile = getInputProfile;
|
|
10
|
+
exports.getInputUsage = getInputUsage;
|
|
11
|
+
exports.getSessionId = getSessionId;
|
|
12
|
+
exports.getContextUsedTokens = getContextUsedTokens;
|
|
13
|
+
exports.getContextLeftPercent = getContextLeftPercent;
|
|
14
|
+
exports.getWorkspaceDir = getWorkspaceDir;
|
|
15
|
+
exports.getGitStatusFromInput = getGitStatusFromInput;
|
|
16
|
+
const fs = require("fs");
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
const type_1 = require("../profile/type");
|
|
19
|
+
const utils_1 = require("./utils");
|
|
20
|
+
function readStdinJson() {
|
|
21
|
+
if (process.stdin.isTTY)
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
const raw = fs.readFileSync(0, "utf8");
|
|
25
|
+
const trimmed = raw.trim();
|
|
26
|
+
if (!trimmed)
|
|
27
|
+
return null;
|
|
28
|
+
const parsed = JSON.parse(trimmed);
|
|
29
|
+
if (!(0, utils_1.isRecord)(parsed))
|
|
30
|
+
return null;
|
|
31
|
+
return parsed;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function normalizeTypeValue(value) {
|
|
38
|
+
if (!value)
|
|
39
|
+
return null;
|
|
40
|
+
const normalized = (0, type_1.normalizeType)(value);
|
|
41
|
+
if (normalized)
|
|
42
|
+
return normalized;
|
|
43
|
+
const trimmed = String(value).trim();
|
|
44
|
+
return trimmed ? trimmed : null;
|
|
45
|
+
}
|
|
46
|
+
function detectTypeFromEnv() {
|
|
47
|
+
const matches = constants_1.DEFAULT_PROFILE_TYPES.filter((type) => {
|
|
48
|
+
const suffix = type.toUpperCase();
|
|
49
|
+
return (process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] ||
|
|
50
|
+
process.env[`CODE_ENV_PROFILE_NAME_${suffix}`]);
|
|
51
|
+
});
|
|
52
|
+
if (matches.length === 1)
|
|
53
|
+
return matches[0];
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
function resolveEnvProfile(type) {
|
|
57
|
+
const genericKey = process.env.CODE_ENV_PROFILE_KEY || null;
|
|
58
|
+
const genericName = process.env.CODE_ENV_PROFILE_NAME || null;
|
|
59
|
+
if (!type) {
|
|
60
|
+
return { key: genericKey, name: genericName };
|
|
61
|
+
}
|
|
62
|
+
const suffix = type.toUpperCase();
|
|
63
|
+
const key = process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] || genericKey;
|
|
64
|
+
const name = process.env[`CODE_ENV_PROFILE_NAME_${suffix}`] || genericName;
|
|
65
|
+
return { key: key || null, name: name || null };
|
|
66
|
+
}
|
|
67
|
+
function getModelFromInput(input) {
|
|
68
|
+
if (!input)
|
|
69
|
+
return null;
|
|
70
|
+
const raw = input.model;
|
|
71
|
+
if (!raw)
|
|
72
|
+
return null;
|
|
73
|
+
if (typeof raw === "string")
|
|
74
|
+
return raw;
|
|
75
|
+
if ((0, utils_1.isRecord)(raw)) {
|
|
76
|
+
const displayName = raw.displayName || raw.display_name;
|
|
77
|
+
if (displayName)
|
|
78
|
+
return String(displayName);
|
|
79
|
+
if (raw.id)
|
|
80
|
+
return String(raw.id);
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
function getModelProviderFromInput(input) {
|
|
85
|
+
if (!input || !input.model_provider)
|
|
86
|
+
return null;
|
|
87
|
+
const provider = String(input.model_provider).trim();
|
|
88
|
+
return provider ? provider : null;
|
|
89
|
+
}
|
|
90
|
+
function getInputProfile(input) {
|
|
91
|
+
if (!input || !(0, utils_1.isRecord)(input.profile))
|
|
92
|
+
return null;
|
|
93
|
+
return input.profile;
|
|
94
|
+
}
|
|
95
|
+
function getInputUsage(input) {
|
|
96
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
97
|
+
if (!input)
|
|
98
|
+
return null;
|
|
99
|
+
if ((0, utils_1.isRecord)(input.usage)) {
|
|
100
|
+
return input.usage;
|
|
101
|
+
}
|
|
102
|
+
const tokenUsage = input.token_usage;
|
|
103
|
+
if (tokenUsage !== null && tokenUsage !== undefined) {
|
|
104
|
+
if (typeof tokenUsage === "number") {
|
|
105
|
+
return {
|
|
106
|
+
todayTokens: null,
|
|
107
|
+
totalTokens: (0, utils_1.coerceNumber)(tokenUsage),
|
|
108
|
+
inputTokens: null,
|
|
109
|
+
outputTokens: null,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if ((0, utils_1.isRecord)(tokenUsage)) {
|
|
113
|
+
const record = tokenUsage;
|
|
114
|
+
const todayTokens = (_a = (0, utils_1.firstNumber)(record.todayTokens, record.today, record.today_tokens, record.daily, record.daily_tokens)) !== null && _a !== void 0 ? _a : null;
|
|
115
|
+
const totalTokens = (_b = (0, utils_1.firstNumber)(record.totalTokens, record.total, record.total_tokens)) !== null && _b !== void 0 ? _b : null;
|
|
116
|
+
const inputTokens = (_c = (0, utils_1.firstNumber)(record.inputTokens, record.input, record.input_tokens)) !== null && _c !== void 0 ? _c : null;
|
|
117
|
+
const outputTokens = (_d = (0, utils_1.firstNumber)(record.outputTokens, record.output, record.output_tokens)) !== null && _d !== void 0 ? _d : null;
|
|
118
|
+
const cacheRead = (_e = (0, utils_1.firstNumber)(record.cache_read_input_tokens, record.cacheReadInputTokens, record.cache_read, record.cacheRead)) !== null && _e !== void 0 ? _e : null;
|
|
119
|
+
const cacheWrite = (_f = (0, utils_1.firstNumber)(record.cache_creation_input_tokens, record.cacheCreationInputTokens, record.cache_write_input_tokens, record.cacheWriteInputTokens, record.cache_write, record.cacheWrite)) !== null && _f !== void 0 ? _f : null;
|
|
120
|
+
if (todayTokens === null &&
|
|
121
|
+
totalTokens === null &&
|
|
122
|
+
inputTokens === null &&
|
|
123
|
+
outputTokens === null &&
|
|
124
|
+
cacheRead === null &&
|
|
125
|
+
cacheWrite === null) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const hasCacheTokens = cacheRead !== null || cacheWrite !== null;
|
|
129
|
+
const computedTotal = hasCacheTokens
|
|
130
|
+
? (inputTokens || 0) +
|
|
131
|
+
(outputTokens || 0) +
|
|
132
|
+
(cacheRead || 0) +
|
|
133
|
+
(cacheWrite || 0)
|
|
134
|
+
: null;
|
|
135
|
+
const resolvedTodayTokens = hasCacheTokens
|
|
136
|
+
? (_g = todayTokens !== null && todayTokens !== void 0 ? todayTokens : totalTokens) !== null && _g !== void 0 ? _g : computedTotal
|
|
137
|
+
: todayTokens;
|
|
138
|
+
return {
|
|
139
|
+
todayTokens: resolvedTodayTokens,
|
|
140
|
+
totalTokens: totalTokens !== null && totalTokens !== void 0 ? totalTokens : null,
|
|
141
|
+
inputTokens,
|
|
142
|
+
outputTokens,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const contextWindow = (0, utils_1.isRecord)(input.context_window)
|
|
147
|
+
? input.context_window
|
|
148
|
+
: (0, utils_1.isRecord)(input.contextWindow)
|
|
149
|
+
? input.contextWindow
|
|
150
|
+
: null;
|
|
151
|
+
if (!contextWindow)
|
|
152
|
+
return null;
|
|
153
|
+
const totalInputTokens = (_h = (0, utils_1.firstNumber)(contextWindow.total_input_tokens, contextWindow.totalInputTokens)) !== null && _h !== void 0 ? _h : null;
|
|
154
|
+
const totalOutputTokens = (_j = (0, utils_1.firstNumber)(contextWindow.total_output_tokens, contextWindow.totalOutputTokens)) !== null && _j !== void 0 ? _j : null;
|
|
155
|
+
if (totalInputTokens !== null || totalOutputTokens !== null) {
|
|
156
|
+
return {
|
|
157
|
+
todayTokens: null,
|
|
158
|
+
totalTokens: null,
|
|
159
|
+
inputTokens: totalInputTokens,
|
|
160
|
+
outputTokens: totalOutputTokens,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const currentUsage = (0, utils_1.isRecord)(contextWindow.current_usage)
|
|
164
|
+
? contextWindow.current_usage
|
|
165
|
+
: (0, utils_1.isRecord)(contextWindow.currentUsage)
|
|
166
|
+
? contextWindow.currentUsage
|
|
167
|
+
: null;
|
|
168
|
+
if (!currentUsage)
|
|
169
|
+
return null;
|
|
170
|
+
const inputTokens = (_k = (0, utils_1.firstNumber)(currentUsage.input_tokens, currentUsage.inputTokens)) !== null && _k !== void 0 ? _k : null;
|
|
171
|
+
const outputTokens = (_l = (0, utils_1.firstNumber)(currentUsage.output_tokens, currentUsage.outputTokens)) !== null && _l !== void 0 ? _l : null;
|
|
172
|
+
const cacheRead = (_m = (0, utils_1.firstNumber)(currentUsage.cache_read_input_tokens, currentUsage.cacheReadInputTokens)) !== null && _m !== void 0 ? _m : null;
|
|
173
|
+
const cacheWrite = (_o = (0, utils_1.firstNumber)(currentUsage.cache_creation_input_tokens, currentUsage.cacheCreationInputTokens)) !== null && _o !== void 0 ? _o : null;
|
|
174
|
+
if (inputTokens === null &&
|
|
175
|
+
outputTokens === null &&
|
|
176
|
+
cacheRead === null &&
|
|
177
|
+
cacheWrite === null) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const totalTokens = (inputTokens || 0) +
|
|
181
|
+
(outputTokens || 0) +
|
|
182
|
+
(cacheRead || 0) +
|
|
183
|
+
(cacheWrite || 0);
|
|
184
|
+
return {
|
|
185
|
+
todayTokens: totalTokens,
|
|
186
|
+
totalTokens: null,
|
|
187
|
+
inputTokens,
|
|
188
|
+
outputTokens,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function getSessionId(input) {
|
|
192
|
+
if (!input)
|
|
193
|
+
return null;
|
|
194
|
+
return (0, utils_1.firstNonEmpty)(input.session_id, input.sessionId);
|
|
195
|
+
}
|
|
196
|
+
function getContextUsedTokens(input) {
|
|
197
|
+
if (!input)
|
|
198
|
+
return null;
|
|
199
|
+
return (0, utils_1.coerceNumber)(input.context_window_used_tokens);
|
|
200
|
+
}
|
|
201
|
+
function getContextLeftPercent(input, type) {
|
|
202
|
+
if (!input)
|
|
203
|
+
return null;
|
|
204
|
+
const raw = (0, utils_1.coerceNumber)(input.context_window_percent);
|
|
205
|
+
if (raw === null || raw < 0)
|
|
206
|
+
return null;
|
|
207
|
+
const percent = raw <= 1 ? raw * 100 : raw;
|
|
208
|
+
if (percent > 100)
|
|
209
|
+
return null;
|
|
210
|
+
const usedTokens = getContextUsedTokens(input);
|
|
211
|
+
const normalizedType = normalizeTypeValue(type);
|
|
212
|
+
const preferRemaining = normalizedType === "codex" ||
|
|
213
|
+
normalizedType === "claude" ||
|
|
214
|
+
usedTokens === null ||
|
|
215
|
+
(usedTokens <= 0 && percent >= 99);
|
|
216
|
+
const left = preferRemaining ? percent : 100 - percent;
|
|
217
|
+
return Math.max(0, Math.min(100, left));
|
|
218
|
+
}
|
|
219
|
+
function getWorkspaceDir(input) {
|
|
220
|
+
if (!input || !(0, utils_1.isRecord)(input.workspace))
|
|
221
|
+
return null;
|
|
222
|
+
const currentDir = input.workspace.current_dir;
|
|
223
|
+
if (currentDir) {
|
|
224
|
+
const trimmed = String(currentDir).trim();
|
|
225
|
+
if (trimmed)
|
|
226
|
+
return trimmed;
|
|
227
|
+
}
|
|
228
|
+
const projectDir = input.workspace.project_dir;
|
|
229
|
+
if (!projectDir)
|
|
230
|
+
return null;
|
|
231
|
+
const trimmed = String(projectDir).trim();
|
|
232
|
+
return trimmed ? trimmed : null;
|
|
233
|
+
}
|
|
234
|
+
function getGitStatusFromInput(input) {
|
|
235
|
+
if (!input || !input.git_branch)
|
|
236
|
+
return null;
|
|
237
|
+
const branch = String(input.git_branch).trim();
|
|
238
|
+
if (!branch)
|
|
239
|
+
return null;
|
|
240
|
+
return {
|
|
241
|
+
branch,
|
|
242
|
+
ahead: 0,
|
|
243
|
+
behind: 0,
|
|
244
|
+
staged: 0,
|
|
245
|
+
unstaged: 0,
|
|
246
|
+
untracked: 0,
|
|
247
|
+
conflicted: 0,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ICON_CWD = exports.ICON_REVIEW = exports.ICON_CONTEXT = exports.ICON_USAGE = exports.ICON_MODEL = exports.ICON_PROFILE = exports.ICON_GIT = void 0;
|
|
4
|
+
exports.colorize = colorize;
|
|
5
|
+
exports.dim = dim;
|
|
6
|
+
const COLOR_ENABLED = !process.env.NO_COLOR && process.env.TERM !== "dumb";
|
|
7
|
+
const ANSI_RESET = "\x1b[0m";
|
|
8
|
+
exports.ICON_GIT = "⎇";
|
|
9
|
+
exports.ICON_PROFILE = "👤";
|
|
10
|
+
exports.ICON_MODEL = "⚙";
|
|
11
|
+
exports.ICON_USAGE = "⚡";
|
|
12
|
+
exports.ICON_CONTEXT = "🧠";
|
|
13
|
+
exports.ICON_REVIEW = "📝";
|
|
14
|
+
exports.ICON_CWD = "📁";
|
|
15
|
+
function colorize(text, colorCode) {
|
|
16
|
+
if (!COLOR_ENABLED)
|
|
17
|
+
return text;
|
|
18
|
+
return `\x1b[${colorCode}m${text}${ANSI_RESET}`;
|
|
19
|
+
}
|
|
20
|
+
function dim(text) {
|
|
21
|
+
return colorize(text, "2");
|
|
22
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseUsageTotalsRecord = parseUsageTotalsRecord;
|
|
4
|
+
exports.getUsageTotalsFromInput = getUsageTotalsFromInput;
|
|
5
|
+
exports.normalizeInputUsage = normalizeInputUsage;
|
|
6
|
+
exports.resolveUsageFromRecords = resolveUsageFromRecords;
|
|
7
|
+
const type_1 = require("../profile/type");
|
|
8
|
+
const usage_1 = require("../usage");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
function parseUsageTotalsRecord(record) {
|
|
11
|
+
var _a, _b, _c, _d, _e;
|
|
12
|
+
const inputTokens = (_a = (0, utils_1.firstNumber)(record.inputTokens, record.input, record.input_tokens)) !== null && _a !== void 0 ? _a : null;
|
|
13
|
+
const outputTokens = (_b = (0, utils_1.firstNumber)(record.outputTokens, record.output, record.output_tokens)) !== null && _b !== void 0 ? _b : null;
|
|
14
|
+
const totalTokens = (_c = (0, utils_1.firstNumber)(record.totalTokens, record.total, record.total_tokens)) !== null && _c !== void 0 ? _c : null;
|
|
15
|
+
const cacheRead = (_d = (0, utils_1.firstNumber)(record.cache_read_input_tokens, record.cacheReadInputTokens, record.cache_read, record.cacheRead)) !== null && _d !== void 0 ? _d : null;
|
|
16
|
+
const cacheWrite = (_e = (0, utils_1.firstNumber)(record.cache_creation_input_tokens, record.cacheCreationInputTokens, record.cache_write_input_tokens, record.cacheWriteInputTokens, record.cache_write, record.cacheWrite)) !== null && _e !== void 0 ? _e : null;
|
|
17
|
+
let computedTotal = null;
|
|
18
|
+
if (inputTokens !== null ||
|
|
19
|
+
outputTokens !== null ||
|
|
20
|
+
cacheRead !== null ||
|
|
21
|
+
cacheWrite !== null) {
|
|
22
|
+
computedTotal =
|
|
23
|
+
(inputTokens || 0) +
|
|
24
|
+
(outputTokens || 0) +
|
|
25
|
+
(cacheRead || 0) +
|
|
26
|
+
(cacheWrite || 0);
|
|
27
|
+
}
|
|
28
|
+
const resolvedTotal = totalTokens !== null && totalTokens !== void 0 ? totalTokens : computedTotal;
|
|
29
|
+
if (inputTokens === null &&
|
|
30
|
+
outputTokens === null &&
|
|
31
|
+
resolvedTotal === null) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
inputTokens,
|
|
36
|
+
outputTokens,
|
|
37
|
+
totalTokens: resolvedTotal,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function getUsageTotalsFromInput(input) {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
if (!input)
|
|
43
|
+
return null;
|
|
44
|
+
const contextWindow = (0, utils_1.isRecord)(input.context_window)
|
|
45
|
+
? input.context_window
|
|
46
|
+
: (0, utils_1.isRecord)(input.contextWindow)
|
|
47
|
+
? input.contextWindow
|
|
48
|
+
: null;
|
|
49
|
+
if (contextWindow) {
|
|
50
|
+
const totalInputTokens = (_a = (0, utils_1.firstNumber)(contextWindow.total_input_tokens, contextWindow.totalInputTokens)) !== null && _a !== void 0 ? _a : null;
|
|
51
|
+
const totalOutputTokens = (_b = (0, utils_1.firstNumber)(contextWindow.total_output_tokens, contextWindow.totalOutputTokens)) !== null && _b !== void 0 ? _b : null;
|
|
52
|
+
if (totalInputTokens !== null || totalOutputTokens !== null) {
|
|
53
|
+
return {
|
|
54
|
+
inputTokens: totalInputTokens,
|
|
55
|
+
outputTokens: totalOutputTokens,
|
|
56
|
+
totalTokens: (totalInputTokens || 0) + (totalOutputTokens || 0),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (typeof input.token_usage === "number") {
|
|
61
|
+
return {
|
|
62
|
+
inputTokens: null,
|
|
63
|
+
outputTokens: null,
|
|
64
|
+
totalTokens: (0, utils_1.coerceNumber)(input.token_usage),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if ((0, utils_1.isRecord)(input.token_usage)) {
|
|
68
|
+
const tokenUsage = input.token_usage;
|
|
69
|
+
const totalUsage = ((0, utils_1.isRecord)(tokenUsage.total_token_usage)
|
|
70
|
+
? tokenUsage.total_token_usage
|
|
71
|
+
: null) ||
|
|
72
|
+
((0, utils_1.isRecord)(tokenUsage.totalTokenUsage)
|
|
73
|
+
? tokenUsage.totalTokenUsage
|
|
74
|
+
: null);
|
|
75
|
+
if (totalUsage) {
|
|
76
|
+
const parsed = parseUsageTotalsRecord(totalUsage);
|
|
77
|
+
if (parsed)
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
return parseUsageTotalsRecord(tokenUsage);
|
|
81
|
+
}
|
|
82
|
+
if ((0, utils_1.isRecord)(input.usage)) {
|
|
83
|
+
return parseUsageTotalsRecord(input.usage);
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
function normalizeInputUsage(inputUsage) {
|
|
88
|
+
if (!inputUsage)
|
|
89
|
+
return null;
|
|
90
|
+
const usage = {
|
|
91
|
+
todayTokens: (0, utils_1.coerceNumber)(inputUsage.todayTokens),
|
|
92
|
+
totalTokens: (0, utils_1.coerceNumber)(inputUsage.totalTokens),
|
|
93
|
+
inputTokens: (0, utils_1.coerceNumber)(inputUsage.inputTokens),
|
|
94
|
+
outputTokens: (0, utils_1.coerceNumber)(inputUsage.outputTokens),
|
|
95
|
+
};
|
|
96
|
+
const hasUsage = usage.todayTokens !== null ||
|
|
97
|
+
usage.totalTokens !== null ||
|
|
98
|
+
usage.inputTokens !== null ||
|
|
99
|
+
usage.outputTokens !== null;
|
|
100
|
+
return hasUsage ? usage : null;
|
|
101
|
+
}
|
|
102
|
+
function resolveUsageFromRecords(config, configPath, type, profileKey, profileName, syncUsage) {
|
|
103
|
+
try {
|
|
104
|
+
const normalized = (0, type_1.normalizeType)(type || "");
|
|
105
|
+
if (!normalized || (!profileKey && !profileName))
|
|
106
|
+
return null;
|
|
107
|
+
const totals = (0, usage_1.readUsageTotalsIndex)(config, configPath, syncUsage);
|
|
108
|
+
if (!totals)
|
|
109
|
+
return null;
|
|
110
|
+
const usage = (0, usage_1.resolveUsageTotalsForProfile)(totals, normalized, profileKey, profileName);
|
|
111
|
+
if (!usage)
|
|
112
|
+
return null;
|
|
113
|
+
return {
|
|
114
|
+
todayTokens: usage.today,
|
|
115
|
+
totalTokens: usage.total,
|
|
116
|
+
inputTokens: null,
|
|
117
|
+
outputTokens: null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRecord = isRecord;
|
|
4
|
+
exports.firstNonEmpty = firstNonEmpty;
|
|
5
|
+
exports.coerceNumber = coerceNumber;
|
|
6
|
+
exports.firstNumber = firstNumber;
|
|
7
|
+
function isRecord(value) {
|
|
8
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
function firstNonEmpty(...values) {
|
|
11
|
+
for (const value of values) {
|
|
12
|
+
if (value === null || value === undefined)
|
|
13
|
+
continue;
|
|
14
|
+
const text = String(value).trim();
|
|
15
|
+
if (text)
|
|
16
|
+
return text;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
function coerceNumber(value) {
|
|
21
|
+
if (value === null || value === undefined || value === "")
|
|
22
|
+
return null;
|
|
23
|
+
const num = Number(value);
|
|
24
|
+
if (!Number.isFinite(num))
|
|
25
|
+
return null;
|
|
26
|
+
return num;
|
|
27
|
+
}
|
|
28
|
+
function firstNumber(...values) {
|
|
29
|
+
for (const value of values) {
|
|
30
|
+
const num = coerceNumber(value);
|
|
31
|
+
if (num !== null)
|
|
32
|
+
return num;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import * as os from "os";
|
|
4
|
+
import { resolvePath } from "../shell/utils";
|
|
5
|
+
|
|
6
|
+
function isStatuslineDebugEnabled(): boolean {
|
|
7
|
+
const raw = process.env.CODE_ENV_STATUSLINE_DEBUG;
|
|
8
|
+
if (!raw) return false;
|
|
9
|
+
const value = String(raw).trim().toLowerCase();
|
|
10
|
+
if (!value) return false;
|
|
11
|
+
return !["0", "false", "no", "off"].includes(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function resolveDefaultConfigDir(configPath: string | null): string {
|
|
15
|
+
if (configPath) return path.dirname(configPath);
|
|
16
|
+
return path.join(os.homedir(), ".config", "code-env");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getStatuslineDebugPath(configPath: string | null): string {
|
|
20
|
+
const envPath = resolvePath(process.env.CODE_ENV_STATUSLINE_DEBUG_PATH);
|
|
21
|
+
if (envPath) return envPath;
|
|
22
|
+
return path.join(resolveDefaultConfigDir(configPath), "statusline-debug.jsonl");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function appendStatuslineDebug(
|
|
26
|
+
configPath: string | null,
|
|
27
|
+
payload: Record<string, unknown>
|
|
28
|
+
) {
|
|
29
|
+
if (!isStatuslineDebugEnabled()) return;
|
|
30
|
+
try {
|
|
31
|
+
const debugPath = getStatuslineDebugPath(configPath);
|
|
32
|
+
const dir = path.dirname(debugPath);
|
|
33
|
+
if (!fs.existsSync(dir)) {
|
|
34
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
35
|
+
}
|
|
36
|
+
fs.appendFileSync(debugPath, `${JSON.stringify(payload)}\n`, "utf8");
|
|
37
|
+
} catch {
|
|
38
|
+
// ignore debug logging failures
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { formatTokenCount } from "../usage";
|
|
2
|
+
import {
|
|
3
|
+
ICON_CONTEXT,
|
|
4
|
+
ICON_CWD,
|
|
5
|
+
ICON_MODEL,
|
|
6
|
+
ICON_PROFILE,
|
|
7
|
+
ICON_REVIEW,
|
|
8
|
+
ICON_USAGE,
|
|
9
|
+
colorize,
|
|
10
|
+
dim,
|
|
11
|
+
} from "./style";
|
|
12
|
+
import type { StatuslineUsage } from "./types";
|
|
13
|
+
import * as path from "path";
|
|
14
|
+
|
|
15
|
+
export function getCwdSegment(cwd: string): string | null {
|
|
16
|
+
if (!cwd) return null;
|
|
17
|
+
const base = path.basename(cwd) || cwd;
|
|
18
|
+
const segment = `${ICON_CWD} ${base}`;
|
|
19
|
+
return dim(segment);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function formatUsageSegment(usage: StatuslineUsage | null): string | null {
|
|
23
|
+
if (!usage) return null;
|
|
24
|
+
const today =
|
|
25
|
+
usage.todayTokens ??
|
|
26
|
+
(usage.inputTokens !== null || usage.outputTokens !== null
|
|
27
|
+
? (usage.inputTokens || 0) + (usage.outputTokens || 0)
|
|
28
|
+
: usage.totalTokens);
|
|
29
|
+
if (today === null) return null;
|
|
30
|
+
const text = `Today ${formatTokenCount(today)}`;
|
|
31
|
+
return colorize(`${ICON_USAGE} ${text}`, "33");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function formatModelSegment(
|
|
35
|
+
model: string | null,
|
|
36
|
+
provider: string | null
|
|
37
|
+
): string | null {
|
|
38
|
+
if (!model) return null;
|
|
39
|
+
const providerLabel = provider ? `${provider}:${model}` : model;
|
|
40
|
+
return colorize(`${ICON_MODEL} ${providerLabel}`, "35");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function formatProfileSegment(
|
|
44
|
+
type: string | null,
|
|
45
|
+
profileKey: string | null,
|
|
46
|
+
profileName: string | null
|
|
47
|
+
): string | null {
|
|
48
|
+
const name = profileName || profileKey;
|
|
49
|
+
if (!name) return null;
|
|
50
|
+
const label = type ? `${type}:${name}` : name;
|
|
51
|
+
return colorize(`${ICON_PROFILE} ${label}`, "37");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function formatContextSegment(contextLeft: number | null): string | null {
|
|
55
|
+
if (contextLeft === null) return null;
|
|
56
|
+
const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
|
|
57
|
+
return colorize(`${ICON_CONTEXT} ${left}% left`, "36");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function formatContextUsedSegment(usedTokens: number | null): string | null {
|
|
61
|
+
if (usedTokens === null) return null;
|
|
62
|
+
return colorize(`${ICON_CONTEXT} ${formatTokenCount(usedTokens)} used`, "36");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function formatModeSegment(reviewMode: boolean): string | null {
|
|
66
|
+
if (!reviewMode) return null;
|
|
67
|
+
return colorize(`${ICON_REVIEW} review`, "34");
|
|
68
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawnSync } from "child_process";
|
|
2
|
+
import type { GitStatus } from "./types";
|
|
3
|
+
import { colorize } from "./style";
|
|
4
|
+
import { ICON_GIT } from "./style";
|
|
5
|
+
|
|
6
|
+
export function getGitStatus(cwd: string): GitStatus | null {
|
|
7
|
+
if (!cwd) return null;
|
|
8
|
+
const result = spawnSync("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
|
|
9
|
+
encoding: "utf8",
|
|
10
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
11
|
+
});
|
|
12
|
+
if (result.status !== 0 || !result.stdout) return null;
|
|
13
|
+
const status: GitStatus = {
|
|
14
|
+
branch: null,
|
|
15
|
+
ahead: 0,
|
|
16
|
+
behind: 0,
|
|
17
|
+
staged: 0,
|
|
18
|
+
unstaged: 0,
|
|
19
|
+
untracked: 0,
|
|
20
|
+
conflicted: 0,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const lines = result.stdout.split(/\r?\n/);
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
if (!line) continue;
|
|
26
|
+
if (line.startsWith("# branch.head ")) {
|
|
27
|
+
status.branch = line.slice("# branch.head ".length).trim();
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (line.startsWith("# branch.ab ")) {
|
|
31
|
+
const parts = line
|
|
32
|
+
.slice("# branch.ab ".length)
|
|
33
|
+
.trim()
|
|
34
|
+
.split(/\s+/);
|
|
35
|
+
for (const part of parts) {
|
|
36
|
+
if (part.startsWith("+")) status.ahead = Number(part.slice(1)) || 0;
|
|
37
|
+
if (part.startsWith("-")) status.behind = Number(part.slice(1)) || 0;
|
|
38
|
+
}
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (line.startsWith("? ")) {
|
|
42
|
+
status.untracked += 1;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (line.startsWith("u ")) {
|
|
46
|
+
status.conflicted += 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (line.startsWith("1 ") || line.startsWith("2 ")) {
|
|
50
|
+
const parts = line.split(/\s+/);
|
|
51
|
+
const xy = parts[1] || "";
|
|
52
|
+
const staged = xy[0];
|
|
53
|
+
const unstaged = xy[1];
|
|
54
|
+
if (staged && staged !== ".") status.staged += 1;
|
|
55
|
+
if (unstaged && unstaged !== ".") status.unstaged += 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!status.branch) {
|
|
61
|
+
status.branch = "HEAD";
|
|
62
|
+
}
|
|
63
|
+
return status;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatGitSegment(status: GitStatus | null): string | null {
|
|
67
|
+
if (!status || !status.branch) return null;
|
|
68
|
+
const meta: string[] = [];
|
|
69
|
+
const dirtyCount = status.staged + status.unstaged + status.untracked;
|
|
70
|
+
if (status.ahead > 0) meta.push(`↑${status.ahead}`);
|
|
71
|
+
if (status.behind > 0) meta.push(`↓${status.behind}`);
|
|
72
|
+
if (status.conflicted > 0) meta.push(`✖${status.conflicted}`);
|
|
73
|
+
if (dirtyCount > 0) meta.push(`+${dirtyCount}`);
|
|
74
|
+
const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
|
|
75
|
+
const text = `${ICON_GIT} ${status.branch}${suffix}`;
|
|
76
|
+
const hasConflicts = status.conflicted > 0;
|
|
77
|
+
const isDirty = dirtyCount > 0;
|
|
78
|
+
if (hasConflicts) return colorize(text, "31");
|
|
79
|
+
if (isDirty) return colorize(text, "33");
|
|
80
|
+
if (status.ahead > 0 || status.behind > 0) return colorize(text, "36");
|
|
81
|
+
return colorize(text, "32");
|
|
82
|
+
}
|