@praeviso/code-env-switch 0.1.1 → 0.1.3

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 (75) hide show
  1. package/.github/workflows/npm-publish.yml +25 -0
  2. package/AGENTS.md +32 -0
  3. package/PLAN.md +33 -0
  4. package/README.md +24 -0
  5. package/README_zh.md +24 -0
  6. package/bin/cli/args.js +303 -0
  7. package/bin/cli/help.js +77 -0
  8. package/bin/cli/index.js +13 -0
  9. package/bin/commands/add.js +81 -0
  10. package/bin/commands/index.js +21 -0
  11. package/bin/commands/launch.js +330 -0
  12. package/bin/commands/list.js +57 -0
  13. package/bin/commands/show.js +10 -0
  14. package/bin/commands/statusline.js +12 -0
  15. package/bin/commands/unset.js +20 -0
  16. package/bin/commands/use.js +92 -0
  17. package/bin/config/defaults.js +85 -0
  18. package/bin/config/index.js +20 -0
  19. package/bin/config/io.js +72 -0
  20. package/bin/constants.js +27 -0
  21. package/bin/index.js +279 -0
  22. package/bin/profile/display.js +78 -0
  23. package/bin/profile/index.js +26 -0
  24. package/bin/profile/match.js +40 -0
  25. package/bin/profile/resolve.js +79 -0
  26. package/bin/profile/type.js +90 -0
  27. package/bin/shell/detect.js +40 -0
  28. package/bin/shell/index.js +18 -0
  29. package/bin/shell/snippet.js +92 -0
  30. package/bin/shell/utils.js +35 -0
  31. package/bin/statusline/claude.js +153 -0
  32. package/bin/statusline/codex.js +356 -0
  33. package/bin/statusline/index.js +631 -0
  34. package/bin/types.js +5 -0
  35. package/bin/ui/index.js +16 -0
  36. package/bin/ui/interactive.js +189 -0
  37. package/bin/ui/readline.js +76 -0
  38. package/bin/usage/index.js +832 -0
  39. package/code-env.example.json +11 -0
  40. package/package.json +2 -2
  41. package/src/cli/args.ts +318 -0
  42. package/src/cli/help.ts +75 -0
  43. package/src/cli/index.ts +5 -0
  44. package/src/commands/add.ts +91 -0
  45. package/src/commands/index.ts +10 -0
  46. package/src/commands/launch.ts +395 -0
  47. package/src/commands/list.ts +91 -0
  48. package/src/commands/show.ts +12 -0
  49. package/src/commands/statusline.ts +18 -0
  50. package/src/commands/unset.ts +19 -0
  51. package/src/commands/use.ts +121 -0
  52. package/src/config/defaults.ts +88 -0
  53. package/src/config/index.ts +19 -0
  54. package/src/config/io.ts +69 -0
  55. package/src/constants.ts +28 -0
  56. package/src/index.ts +359 -0
  57. package/src/profile/display.ts +77 -0
  58. package/src/profile/index.ts +12 -0
  59. package/src/profile/match.ts +41 -0
  60. package/src/profile/resolve.ts +84 -0
  61. package/src/profile/type.ts +83 -0
  62. package/src/shell/detect.ts +30 -0
  63. package/src/shell/index.ts +6 -0
  64. package/src/shell/snippet.ts +92 -0
  65. package/src/shell/utils.ts +30 -0
  66. package/src/statusline/claude.ts +172 -0
  67. package/src/statusline/codex.ts +393 -0
  68. package/src/statusline/index.ts +920 -0
  69. package/src/types.ts +95 -0
  70. package/src/ui/index.ts +5 -0
  71. package/src/ui/interactive.ts +220 -0
  72. package/src/ui/readline.ts +85 -0
  73. package/src/usage/index.ts +979 -0
  74. package/bin/codenv.js +0 -1316
  75. package/src/codenv.ts +0 -1478
@@ -0,0 +1,631 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildStatuslineResult = buildStatuslineResult;
4
+ /**
5
+ * Statusline builder
6
+ */
7
+ const fs = require("fs");
8
+ const path = require("path");
9
+ const child_process_1 = require("child_process");
10
+ const constants_1 = require("../constants");
11
+ const type_1 = require("../profile/type");
12
+ const usage_1 = require("../usage");
13
+ const COLOR_ENABLED = !process.env.NO_COLOR && process.env.TERM !== "dumb";
14
+ const ANSI_RESET = "\x1b[0m";
15
+ const ICON_GIT = "⎇";
16
+ const ICON_PROFILE = "👤";
17
+ const ICON_MODEL = "⚙";
18
+ const ICON_USAGE = "⚡";
19
+ const ICON_CONTEXT = "🧠";
20
+ const ICON_REVIEW = "📝";
21
+ const ICON_CWD = "📁";
22
+ function colorize(text, colorCode) {
23
+ if (!COLOR_ENABLED)
24
+ return text;
25
+ return `\x1b[${colorCode}m${text}${ANSI_RESET}`;
26
+ }
27
+ function dim(text) {
28
+ return colorize(text, "2");
29
+ }
30
+ function getCwdSegment(cwd) {
31
+ if (!cwd)
32
+ return null;
33
+ const base = path.basename(cwd) || cwd;
34
+ const segment = `${ICON_CWD} ${base}`;
35
+ return dim(segment);
36
+ }
37
+ function isRecord(value) {
38
+ return typeof value === "object" && value !== null && !Array.isArray(value);
39
+ }
40
+ function readStdinJson() {
41
+ if (process.stdin.isTTY)
42
+ return null;
43
+ try {
44
+ const raw = fs.readFileSync(0, "utf8");
45
+ const trimmed = raw.trim();
46
+ if (!trimmed)
47
+ return null;
48
+ const parsed = JSON.parse(trimmed);
49
+ if (!isRecord(parsed))
50
+ return null;
51
+ return parsed;
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ }
57
+ function firstNonEmpty(...values) {
58
+ for (const value of values) {
59
+ if (value === null || value === undefined)
60
+ continue;
61
+ const text = String(value).trim();
62
+ if (text)
63
+ return text;
64
+ }
65
+ return null;
66
+ }
67
+ function coerceNumber(value) {
68
+ if (value === null || value === undefined || value === "")
69
+ return null;
70
+ const num = Number(value);
71
+ if (!Number.isFinite(num))
72
+ return null;
73
+ return num;
74
+ }
75
+ function firstNumber(...values) {
76
+ for (const value of values) {
77
+ const num = coerceNumber(value);
78
+ if (num !== null)
79
+ return num;
80
+ }
81
+ return null;
82
+ }
83
+ function normalizeTypeValue(value) {
84
+ if (!value)
85
+ return null;
86
+ const normalized = (0, type_1.normalizeType)(value);
87
+ if (normalized)
88
+ return normalized;
89
+ const trimmed = String(value).trim();
90
+ return trimmed ? trimmed : null;
91
+ }
92
+ function detectTypeFromEnv() {
93
+ const matches = constants_1.DEFAULT_PROFILE_TYPES.filter((type) => {
94
+ const suffix = type.toUpperCase();
95
+ return (process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] ||
96
+ process.env[`CODE_ENV_PROFILE_NAME_${suffix}`]);
97
+ });
98
+ if (matches.length === 1)
99
+ return matches[0];
100
+ return null;
101
+ }
102
+ function resolveEnvProfile(type) {
103
+ const genericKey = process.env.CODE_ENV_PROFILE_KEY || null;
104
+ const genericName = process.env.CODE_ENV_PROFILE_NAME || null;
105
+ if (!type) {
106
+ return { key: genericKey, name: genericName };
107
+ }
108
+ const suffix = type.toUpperCase();
109
+ const key = process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] || genericKey;
110
+ const name = process.env[`CODE_ENV_PROFILE_NAME_${suffix}`] || genericName;
111
+ return { key: key || null, name: name || null };
112
+ }
113
+ function getModelFromInput(input) {
114
+ if (!input)
115
+ return null;
116
+ const raw = input.model;
117
+ if (!raw)
118
+ return null;
119
+ if (typeof raw === "string")
120
+ return raw;
121
+ if (isRecord(raw)) {
122
+ const displayName = raw.displayName || raw.display_name;
123
+ if (displayName)
124
+ return String(displayName);
125
+ if (raw.id)
126
+ return String(raw.id);
127
+ }
128
+ return null;
129
+ }
130
+ function getModelProviderFromInput(input) {
131
+ if (!input || !input.model_provider)
132
+ return null;
133
+ const provider = String(input.model_provider).trim();
134
+ return provider ? provider : null;
135
+ }
136
+ function getInputProfile(input) {
137
+ if (!input || !isRecord(input.profile))
138
+ return null;
139
+ return input.profile;
140
+ }
141
+ function getInputUsage(input) {
142
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
143
+ if (!input)
144
+ return null;
145
+ if (isRecord(input.usage)) {
146
+ return input.usage;
147
+ }
148
+ const tokenUsage = input.token_usage;
149
+ if (tokenUsage !== null && tokenUsage !== undefined) {
150
+ if (typeof tokenUsage === "number") {
151
+ return {
152
+ todayTokens: null,
153
+ totalTokens: coerceNumber(tokenUsage),
154
+ inputTokens: null,
155
+ outputTokens: null,
156
+ };
157
+ }
158
+ if (isRecord(tokenUsage)) {
159
+ const record = tokenUsage;
160
+ const todayTokens = (_a = firstNumber(record.todayTokens, record.today, record.today_tokens, record.daily, record.daily_tokens)) !== null && _a !== void 0 ? _a : null;
161
+ const totalTokens = (_b = firstNumber(record.totalTokens, record.total, record.total_tokens)) !== null && _b !== void 0 ? _b : null;
162
+ const inputTokens = (_c = firstNumber(record.inputTokens, record.input, record.input_tokens)) !== null && _c !== void 0 ? _c : null;
163
+ const outputTokens = (_d = firstNumber(record.outputTokens, record.output, record.output_tokens)) !== null && _d !== void 0 ? _d : null;
164
+ const cacheRead = (_e = firstNumber(record.cache_read_input_tokens, record.cacheReadInputTokens, record.cache_read, record.cacheRead)) !== null && _e !== void 0 ? _e : null;
165
+ const cacheWrite = (_f = 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;
166
+ if (todayTokens === null &&
167
+ totalTokens === null &&
168
+ inputTokens === null &&
169
+ outputTokens === null &&
170
+ cacheRead === null &&
171
+ cacheWrite === null) {
172
+ return null;
173
+ }
174
+ const hasCacheTokens = cacheRead !== null || cacheWrite !== null;
175
+ const computedTotal = hasCacheTokens
176
+ ? (inputTokens || 0) +
177
+ (outputTokens || 0) +
178
+ (cacheRead || 0) +
179
+ (cacheWrite || 0)
180
+ : null;
181
+ const resolvedTodayTokens = hasCacheTokens
182
+ ? (_g = todayTokens !== null && todayTokens !== void 0 ? todayTokens : totalTokens) !== null && _g !== void 0 ? _g : computedTotal
183
+ : todayTokens;
184
+ return {
185
+ todayTokens: resolvedTodayTokens,
186
+ totalTokens: totalTokens !== null && totalTokens !== void 0 ? totalTokens : null,
187
+ inputTokens,
188
+ outputTokens,
189
+ };
190
+ }
191
+ }
192
+ const contextWindow = isRecord(input.context_window)
193
+ ? input.context_window
194
+ : isRecord(input.contextWindow)
195
+ ? input.contextWindow
196
+ : null;
197
+ if (!contextWindow)
198
+ return null;
199
+ const totalInputTokens = (_h = firstNumber(contextWindow.total_input_tokens, contextWindow.totalInputTokens)) !== null && _h !== void 0 ? _h : null;
200
+ const totalOutputTokens = (_j = firstNumber(contextWindow.total_output_tokens, contextWindow.totalOutputTokens)) !== null && _j !== void 0 ? _j : null;
201
+ if (totalInputTokens !== null || totalOutputTokens !== null) {
202
+ return {
203
+ todayTokens: null,
204
+ totalTokens: null,
205
+ inputTokens: totalInputTokens,
206
+ outputTokens: totalOutputTokens,
207
+ };
208
+ }
209
+ const currentUsage = isRecord(contextWindow.current_usage)
210
+ ? contextWindow.current_usage
211
+ : isRecord(contextWindow.currentUsage)
212
+ ? contextWindow.currentUsage
213
+ : null;
214
+ if (!currentUsage)
215
+ return null;
216
+ const inputTokens = (_k = firstNumber(currentUsage.input_tokens, currentUsage.inputTokens)) !== null && _k !== void 0 ? _k : null;
217
+ const outputTokens = (_l = firstNumber(currentUsage.output_tokens, currentUsage.outputTokens)) !== null && _l !== void 0 ? _l : null;
218
+ const cacheRead = (_m = firstNumber(currentUsage.cache_read_input_tokens, currentUsage.cacheReadInputTokens)) !== null && _m !== void 0 ? _m : null;
219
+ const cacheWrite = (_o = firstNumber(currentUsage.cache_creation_input_tokens, currentUsage.cacheCreationInputTokens)) !== null && _o !== void 0 ? _o : null;
220
+ if (inputTokens === null &&
221
+ outputTokens === null &&
222
+ cacheRead === null &&
223
+ cacheWrite === null) {
224
+ return null;
225
+ }
226
+ const totalTokens = (inputTokens || 0) +
227
+ (outputTokens || 0) +
228
+ (cacheRead || 0) +
229
+ (cacheWrite || 0);
230
+ return {
231
+ todayTokens: totalTokens,
232
+ totalTokens: null,
233
+ inputTokens,
234
+ outputTokens,
235
+ };
236
+ }
237
+ function getSessionId(input) {
238
+ if (!input)
239
+ return null;
240
+ return firstNonEmpty(input.session_id, input.sessionId);
241
+ }
242
+ function parseUsageTotalsRecord(record) {
243
+ var _a, _b, _c, _d, _e;
244
+ const inputTokens = (_a = firstNumber(record.inputTokens, record.input, record.input_tokens)) !== null && _a !== void 0 ? _a : null;
245
+ const outputTokens = (_b = firstNumber(record.outputTokens, record.output, record.output_tokens)) !== null && _b !== void 0 ? _b : null;
246
+ const totalTokens = (_c = firstNumber(record.totalTokens, record.total, record.total_tokens)) !== null && _c !== void 0 ? _c : null;
247
+ const cacheRead = (_d = firstNumber(record.cache_read_input_tokens, record.cacheReadInputTokens, record.cache_read, record.cacheRead)) !== null && _d !== void 0 ? _d : null;
248
+ const cacheWrite = (_e = 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;
249
+ let computedTotal = null;
250
+ if (inputTokens !== null ||
251
+ outputTokens !== null ||
252
+ cacheRead !== null ||
253
+ cacheWrite !== null) {
254
+ computedTotal =
255
+ (inputTokens || 0) +
256
+ (outputTokens || 0) +
257
+ (cacheRead || 0) +
258
+ (cacheWrite || 0);
259
+ }
260
+ const resolvedTotal = totalTokens !== null && totalTokens !== void 0 ? totalTokens : computedTotal;
261
+ if (inputTokens === null &&
262
+ outputTokens === null &&
263
+ resolvedTotal === null) {
264
+ return null;
265
+ }
266
+ return {
267
+ inputTokens,
268
+ outputTokens,
269
+ totalTokens: resolvedTotal,
270
+ };
271
+ }
272
+ function getUsageTotalsFromInput(input) {
273
+ var _a, _b;
274
+ if (!input)
275
+ return null;
276
+ const contextWindow = isRecord(input.context_window)
277
+ ? input.context_window
278
+ : isRecord(input.contextWindow)
279
+ ? input.contextWindow
280
+ : null;
281
+ if (contextWindow) {
282
+ const totalInputTokens = (_a = firstNumber(contextWindow.total_input_tokens, contextWindow.totalInputTokens)) !== null && _a !== void 0 ? _a : null;
283
+ const totalOutputTokens = (_b = firstNumber(contextWindow.total_output_tokens, contextWindow.totalOutputTokens)) !== null && _b !== void 0 ? _b : null;
284
+ if (totalInputTokens !== null || totalOutputTokens !== null) {
285
+ return {
286
+ inputTokens: totalInputTokens,
287
+ outputTokens: totalOutputTokens,
288
+ totalTokens: (totalInputTokens || 0) + (totalOutputTokens || 0),
289
+ };
290
+ }
291
+ }
292
+ if (typeof input.token_usage === "number") {
293
+ return {
294
+ inputTokens: null,
295
+ outputTokens: null,
296
+ totalTokens: coerceNumber(input.token_usage),
297
+ };
298
+ }
299
+ if (isRecord(input.token_usage)) {
300
+ return parseUsageTotalsRecord(input.token_usage);
301
+ }
302
+ if (isRecord(input.usage)) {
303
+ return parseUsageTotalsRecord(input.usage);
304
+ }
305
+ return null;
306
+ }
307
+ function getContextUsedTokens(input) {
308
+ if (!input)
309
+ return null;
310
+ return coerceNumber(input.context_window_used_tokens);
311
+ }
312
+ function normalizeInputUsage(inputUsage) {
313
+ if (!inputUsage)
314
+ return null;
315
+ const usage = {
316
+ todayTokens: coerceNumber(inputUsage.todayTokens),
317
+ totalTokens: coerceNumber(inputUsage.totalTokens),
318
+ inputTokens: coerceNumber(inputUsage.inputTokens),
319
+ outputTokens: coerceNumber(inputUsage.outputTokens),
320
+ };
321
+ const hasUsage = usage.todayTokens !== null ||
322
+ usage.totalTokens !== null ||
323
+ usage.inputTokens !== null ||
324
+ usage.outputTokens !== null;
325
+ return hasUsage ? usage : null;
326
+ }
327
+ function getContextLeftPercent(input, type) {
328
+ if (!input)
329
+ return null;
330
+ const raw = coerceNumber(input.context_window_percent);
331
+ if (raw === null || raw < 0)
332
+ return null;
333
+ const percent = raw <= 1 ? raw * 100 : raw;
334
+ if (percent > 100)
335
+ return null;
336
+ const usedTokens = getContextUsedTokens(input);
337
+ const normalizedType = normalizeTypeValue(type);
338
+ // Prefer treating the percent as "remaining" for codex/claude and when usage is absent.
339
+ const preferRemaining = normalizedType === "codex" ||
340
+ normalizedType === "claude" ||
341
+ usedTokens === null ||
342
+ (usedTokens <= 0 && percent >= 99);
343
+ const left = preferRemaining ? percent : 100 - percent;
344
+ return Math.max(0, Math.min(100, left));
345
+ }
346
+ function getWorkspaceDir(input) {
347
+ if (!input || !isRecord(input.workspace))
348
+ return null;
349
+ const currentDir = input.workspace.current_dir;
350
+ if (currentDir) {
351
+ const trimmed = String(currentDir).trim();
352
+ if (trimmed)
353
+ return trimmed;
354
+ }
355
+ const projectDir = input.workspace.project_dir;
356
+ if (!projectDir)
357
+ return null;
358
+ const trimmed = String(projectDir).trim();
359
+ return trimmed ? trimmed : null;
360
+ }
361
+ function getGitStatusFromInput(input) {
362
+ if (!input || !input.git_branch)
363
+ return null;
364
+ const branch = String(input.git_branch).trim();
365
+ if (!branch)
366
+ return null;
367
+ return {
368
+ branch,
369
+ ahead: 0,
370
+ behind: 0,
371
+ staged: 0,
372
+ unstaged: 0,
373
+ untracked: 0,
374
+ conflicted: 0,
375
+ };
376
+ }
377
+ function getGitStatus(cwd) {
378
+ if (!cwd)
379
+ return null;
380
+ const result = (0, child_process_1.spawnSync)("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
381
+ encoding: "utf8",
382
+ stdio: ["ignore", "pipe", "ignore"],
383
+ });
384
+ if (result.status !== 0 || !result.stdout)
385
+ return null;
386
+ const status = {
387
+ branch: null,
388
+ ahead: 0,
389
+ behind: 0,
390
+ staged: 0,
391
+ unstaged: 0,
392
+ untracked: 0,
393
+ conflicted: 0,
394
+ };
395
+ const lines = result.stdout.split(/\r?\n/);
396
+ for (const line of lines) {
397
+ if (!line)
398
+ continue;
399
+ if (line.startsWith("# branch.head ")) {
400
+ status.branch = line.slice("# branch.head ".length).trim();
401
+ continue;
402
+ }
403
+ if (line.startsWith("# branch.ab ")) {
404
+ const parts = line
405
+ .slice("# branch.ab ".length)
406
+ .trim()
407
+ .split(/\s+/);
408
+ for (const part of parts) {
409
+ if (part.startsWith("+"))
410
+ status.ahead = Number(part.slice(1)) || 0;
411
+ if (part.startsWith("-"))
412
+ status.behind = Number(part.slice(1)) || 0;
413
+ }
414
+ continue;
415
+ }
416
+ if (line.startsWith("? ")) {
417
+ status.untracked += 1;
418
+ continue;
419
+ }
420
+ if (line.startsWith("u ")) {
421
+ status.conflicted += 1;
422
+ continue;
423
+ }
424
+ if (line.startsWith("1 ") || line.startsWith("2 ")) {
425
+ const parts = line.split(/\s+/);
426
+ const xy = parts[1] || "";
427
+ const staged = xy[0];
428
+ const unstaged = xy[1];
429
+ if (staged && staged !== ".")
430
+ status.staged += 1;
431
+ if (unstaged && unstaged !== ".")
432
+ status.unstaged += 1;
433
+ continue;
434
+ }
435
+ }
436
+ if (!status.branch) {
437
+ status.branch = "HEAD";
438
+ }
439
+ return status;
440
+ }
441
+ function formatGitSegment(status) {
442
+ if (!status || !status.branch)
443
+ return null;
444
+ const meta = [];
445
+ const dirtyCount = status.staged + status.unstaged + status.untracked;
446
+ if (status.ahead > 0)
447
+ meta.push(`↑${status.ahead}`);
448
+ if (status.behind > 0)
449
+ meta.push(`↓${status.behind}`);
450
+ if (status.conflicted > 0)
451
+ meta.push(`✖${status.conflicted}`);
452
+ if (dirtyCount > 0)
453
+ meta.push(`+${dirtyCount}`);
454
+ const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
455
+ const text = `${ICON_GIT} ${status.branch}${suffix}`;
456
+ const hasConflicts = status.conflicted > 0;
457
+ const isDirty = dirtyCount > 0;
458
+ if (hasConflicts)
459
+ return colorize(text, "31");
460
+ if (isDirty)
461
+ return colorize(text, "33");
462
+ if (status.ahead > 0 || status.behind > 0)
463
+ return colorize(text, "36");
464
+ return colorize(text, "32");
465
+ }
466
+ function resolveUsageFromRecords(config, configPath, type, profileKey, profileName, syncUsage) {
467
+ try {
468
+ const normalized = (0, type_1.normalizeType)(type || "");
469
+ if (!normalized || (!profileKey && !profileName))
470
+ return null;
471
+ const totals = (0, usage_1.readUsageTotalsIndex)(config, configPath, syncUsage);
472
+ if (!totals)
473
+ return null;
474
+ const usage = (0, usage_1.resolveUsageTotalsForProfile)(totals, normalized, profileKey, profileName);
475
+ if (!usage)
476
+ return null;
477
+ return {
478
+ todayTokens: usage.today,
479
+ totalTokens: usage.total,
480
+ inputTokens: null,
481
+ outputTokens: null,
482
+ };
483
+ }
484
+ catch {
485
+ return null;
486
+ }
487
+ }
488
+ function formatUsageSegment(usage) {
489
+ var _a;
490
+ if (!usage)
491
+ return null;
492
+ const today = (_a = usage.todayTokens) !== null && _a !== void 0 ? _a : (usage.inputTokens !== null || usage.outputTokens !== null
493
+ ? (usage.inputTokens || 0) + (usage.outputTokens || 0)
494
+ : usage.totalTokens);
495
+ if (today === null)
496
+ return null;
497
+ const text = `Today ${(0, usage_1.formatTokenCount)(today)}`;
498
+ return colorize(`${ICON_USAGE} ${text}`, "33");
499
+ }
500
+ function formatModelSegment(model, provider) {
501
+ if (!model)
502
+ return null;
503
+ const providerLabel = provider ? `${provider}:${model}` : model;
504
+ return colorize(`${ICON_MODEL} ${providerLabel}`, "35");
505
+ }
506
+ function formatProfileSegment(type, profileKey, profileName) {
507
+ const name = profileName || profileKey;
508
+ if (!name)
509
+ return null;
510
+ const label = type ? `${type}:${name}` : name;
511
+ return colorize(`${ICON_PROFILE} ${label}`, "37");
512
+ }
513
+ function formatContextSegment(contextLeft) {
514
+ if (contextLeft === null)
515
+ return null;
516
+ const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
517
+ return colorize(`${ICON_CONTEXT} ${left}% left`, "36");
518
+ }
519
+ function formatContextUsedSegment(usedTokens) {
520
+ if (usedTokens === null)
521
+ return null;
522
+ return colorize(`${ICON_CONTEXT} ${(0, usage_1.formatTokenCount)(usedTokens)} used`, "36");
523
+ }
524
+ function formatModeSegment(reviewMode) {
525
+ if (!reviewMode)
526
+ return null;
527
+ return colorize(`${ICON_REVIEW} review`, "34");
528
+ }
529
+ function buildStatuslineResult(args, config, configPath) {
530
+ const stdinInput = readStdinJson();
531
+ const inputProfile = getInputProfile(stdinInput);
532
+ let typeCandidate = firstNonEmpty(args.type, process.env.CODE_ENV_TYPE, inputProfile ? inputProfile.type : null, stdinInput ? stdinInput.type : null);
533
+ if (!typeCandidate) {
534
+ typeCandidate = detectTypeFromEnv();
535
+ }
536
+ let type = normalizeTypeValue(typeCandidate);
537
+ const envProfile = resolveEnvProfile(type);
538
+ const profileKey = firstNonEmpty(args.profileKey, envProfile.key, inputProfile ? inputProfile.key : null);
539
+ let profileName = firstNonEmpty(args.profileName, envProfile.name, inputProfile ? inputProfile.name : null);
540
+ if (profileKey && !profileName && config.profiles && config.profiles[profileKey]) {
541
+ const profile = config.profiles[profileKey];
542
+ profileName = (0, type_1.getProfileDisplayName)(profileKey, profile, type || undefined);
543
+ if (!type) {
544
+ const inferred = (0, type_1.inferProfileType)(profileKey, profile, null);
545
+ if (inferred)
546
+ type = inferred;
547
+ }
548
+ }
549
+ if (!type && profileKey && config.profiles && config.profiles[profileKey]) {
550
+ const profile = config.profiles[profileKey];
551
+ const inferred = (0, type_1.inferProfileType)(profileKey, profile, null);
552
+ if (inferred)
553
+ type = inferred;
554
+ }
555
+ const cwd = firstNonEmpty(args.cwd, process.env.CODE_ENV_CWD, getWorkspaceDir(stdinInput), stdinInput ? stdinInput.cwd : null, process.cwd());
556
+ const sessionId = getSessionId(stdinInput);
557
+ const stdinUsageTotals = getUsageTotalsFromInput(stdinInput);
558
+ if (args.syncUsage && sessionId && stdinUsageTotals) {
559
+ const usageType = (0, type_1.normalizeType)(type || "");
560
+ (0, usage_1.syncUsageFromStatuslineInput)(config, configPath, usageType, profileKey, profileName, sessionId, stdinUsageTotals, cwd);
561
+ }
562
+ const model = firstNonEmpty(args.model, process.env.CODE_ENV_MODEL, getModelFromInput(stdinInput));
563
+ const modelProvider = firstNonEmpty(process.env.CODE_ENV_MODEL_PROVIDER, getModelProviderFromInput(stdinInput));
564
+ const usage = {
565
+ todayTokens: firstNumber(args.usageToday, process.env.CODE_ENV_USAGE_TODAY),
566
+ totalTokens: firstNumber(args.usageTotal, process.env.CODE_ENV_USAGE_TOTAL),
567
+ inputTokens: firstNumber(args.usageInput, process.env.CODE_ENV_USAGE_INPUT),
568
+ outputTokens: firstNumber(args.usageOutput, process.env.CODE_ENV_USAGE_OUTPUT),
569
+ };
570
+ const hasExplicitUsage = usage.todayTokens !== null ||
571
+ usage.totalTokens !== null ||
572
+ usage.inputTokens !== null ||
573
+ usage.outputTokens !== null;
574
+ const stdinUsage = normalizeInputUsage(getInputUsage(stdinInput));
575
+ let finalUsage = hasExplicitUsage ? usage : null;
576
+ if (!finalUsage) {
577
+ finalUsage = stdinUsage;
578
+ }
579
+ if (!finalUsage) {
580
+ finalUsage = resolveUsageFromRecords(config, configPath, type, profileKey, profileName, args.syncUsage);
581
+ }
582
+ let gitStatus = getGitStatus(cwd);
583
+ if (!gitStatus) {
584
+ gitStatus = getGitStatusFromInput(stdinInput);
585
+ }
586
+ else {
587
+ const inputGit = getGitStatusFromInput(stdinInput);
588
+ if (inputGit && (!gitStatus.branch || gitStatus.branch === "HEAD")) {
589
+ gitStatus.branch = inputGit.branch;
590
+ }
591
+ }
592
+ const gitSegment = formatGitSegment(gitStatus);
593
+ const profileSegment = formatProfileSegment(type, profileKey, profileName);
594
+ const modelSegment = formatModelSegment(model, modelProvider);
595
+ const usageSegment = formatUsageSegment(finalUsage);
596
+ const contextLeft = getContextLeftPercent(stdinInput, type);
597
+ const contextSegment = formatContextSegment(contextLeft);
598
+ const contextUsedTokens = getContextUsedTokens(stdinInput);
599
+ const contextUsedSegment = contextSegment === null ? formatContextUsedSegment(contextUsedTokens) : null;
600
+ const modeSegment = formatModeSegment((stdinInput === null || stdinInput === void 0 ? void 0 : stdinInput.review_mode) === true);
601
+ const cwdSegment = getCwdSegment(cwd);
602
+ const segments = [];
603
+ if (gitSegment)
604
+ segments.push(gitSegment);
605
+ if (profileSegment)
606
+ segments.push(profileSegment);
607
+ if (modeSegment)
608
+ segments.push(modeSegment);
609
+ if (modelSegment)
610
+ segments.push(modelSegment);
611
+ if (usageSegment)
612
+ segments.push(usageSegment);
613
+ if (contextSegment)
614
+ segments.push(contextSegment);
615
+ if (contextUsedSegment)
616
+ segments.push(contextUsedSegment);
617
+ if (cwdSegment)
618
+ segments.push(cwdSegment);
619
+ const text = segments.join(" ");
620
+ return {
621
+ text,
622
+ json: {
623
+ cwd,
624
+ type,
625
+ profile: { key: profileKey, name: profileName },
626
+ model,
627
+ usage: finalUsage,
628
+ git: gitStatus,
629
+ },
630
+ };
631
+ }
package/bin/types.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * Type definitions for codenv
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runInteractiveUse = exports.runInteractiveAdd = exports.askProfileName = exports.askType = exports.askConfirm = exports.askRequired = exports.ask = exports.createReadline = void 0;
4
+ /**
5
+ * UI module exports
6
+ */
7
+ var readline_1 = require("./readline");
8
+ Object.defineProperty(exports, "createReadline", { enumerable: true, get: function () { return readline_1.createReadline; } });
9
+ Object.defineProperty(exports, "ask", { enumerable: true, get: function () { return readline_1.ask; } });
10
+ Object.defineProperty(exports, "askRequired", { enumerable: true, get: function () { return readline_1.askRequired; } });
11
+ Object.defineProperty(exports, "askConfirm", { enumerable: true, get: function () { return readline_1.askConfirm; } });
12
+ Object.defineProperty(exports, "askType", { enumerable: true, get: function () { return readline_1.askType; } });
13
+ Object.defineProperty(exports, "askProfileName", { enumerable: true, get: function () { return readline_1.askProfileName; } });
14
+ var interactive_1 = require("./interactive");
15
+ Object.defineProperty(exports, "runInteractiveAdd", { enumerable: true, get: function () { return interactive_1.runInteractiveAdd; } });
16
+ Object.defineProperty(exports, "runInteractiveUse", { enumerable: true, get: function () { return interactive_1.runInteractiveUse; } });