@praeviso/code-env-switch 0.1.3 → 0.1.5
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/README.md +17 -0
- package/README_zh.md +17 -0
- package/bin/commands/list.js +44 -2
- package/bin/statusline/debug.js +42 -0
- package/bin/statusline/format.js +57 -0
- package/bin/statusline/git.js +96 -0
- package/bin/statusline/index.js +107 -558
- package/bin/statusline/input.js +167 -0
- package/bin/statusline/style.js +22 -0
- package/bin/statusline/types.js +2 -0
- package/bin/statusline/usage/claude.js +181 -0
- package/bin/statusline/usage/codex.js +168 -0
- package/bin/statusline/usage.js +67 -0
- package/bin/statusline/utils.js +35 -0
- package/bin/usage/index.js +396 -41
- package/bin/usage/pricing.js +303 -0
- package/code-env.example.json +55 -0
- package/package.json +1 -1
- package/src/commands/list.ts +74 -4
- package/src/statusline/debug.ts +40 -0
- package/src/statusline/format.ts +67 -0
- package/src/statusline/git.ts +82 -0
- package/src/statusline/index.ts +143 -764
- package/src/statusline/input.ts +159 -0
- package/src/statusline/style.ts +19 -0
- package/src/statusline/types.ts +111 -0
- package/src/statusline/usage/claude.ts +299 -0
- package/src/statusline/usage/codex.ts +263 -0
- package/src/statusline/usage.ts +80 -0
- package/src/statusline/utils.ts +27 -0
- package/src/types.ts +23 -0
- package/src/usage/index.ts +519 -35
- package/src/usage/pricing.ts +323 -0
- package/PLAN.md +0 -33
package/README.md
CHANGED
|
@@ -217,6 +217,17 @@ If nothing is found, `codenv add` writes to `~/.config/code-env/config.json`.
|
|
|
217
217
|
"type": "command",
|
|
218
218
|
"padding": 0
|
|
219
219
|
},
|
|
220
|
+
"pricing": {
|
|
221
|
+
"models": {
|
|
222
|
+
"Claude Sonnet 4.5": {
|
|
223
|
+
"input": 3.0,
|
|
224
|
+
"output": 15.0,
|
|
225
|
+
"cacheWrite": 3.75,
|
|
226
|
+
"cacheRead": 0.3,
|
|
227
|
+
"description": "Balanced performance and speed for daily use."
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
},
|
|
220
231
|
"profiles": {
|
|
221
232
|
"p_a1b2c3": {
|
|
222
233
|
"name": "primary",
|
|
@@ -247,10 +258,16 @@ Notes:
|
|
|
247
258
|
- `type`: string; statusLine type (default: `command`).
|
|
248
259
|
- `padding`: number; statusLine padding (default: 0).
|
|
249
260
|
- `settingsPath`: optional; override `~/.claude/settings.json` (also supports `CODE_ENV_CLAUDE_SETTINGS_PATH`).
|
|
261
|
+
- `pricing`: optional; model pricing (USD per 1M tokens) used to convert token usage to dollar amounts in the status line.
|
|
262
|
+
- `models`: map of model name to pricing. Keys are matched case/format-insensitively.
|
|
263
|
+
- `input`/`output`/`cacheRead`/`cacheWrite`: token rates.
|
|
264
|
+
- Cost display uses the profile pricing model if set; otherwise the status line model label. No breakdown => no cost.
|
|
250
265
|
- `name`: human-facing profile name shown in `codenv list` and used by `codenv use <name>`.
|
|
251
266
|
- `type`: optional; `codex` or `claude` (alias `cc`) for `codenv use <type> <name>` matching.
|
|
252
267
|
- `note`: shown in `codenv list`.
|
|
253
268
|
- `removeFiles`: optional; `codenv use` emits `rm -f` for each path. Codex profiles also remove `~/.codex/auth.json`.
|
|
269
|
+
- `pricing` (profile): optional; per-profile pricing override. Supports `model` plus `input`/`output`/`cacheRead`/`cacheWrite`.
|
|
270
|
+
- `multiplier`: optional; scale pricing (number).
|
|
254
271
|
- `ANTHROPIC_AUTH_TOKEN`: when `ANTHROPIC_API_KEY` is set, `codenv use` also exports `ANTHROPIC_AUTH_TOKEN` with the same value.
|
|
255
272
|
- `commands`: optional; emitted as-is in the switch script.
|
|
256
273
|
|
package/README_zh.md
CHANGED
|
@@ -216,6 +216,17 @@ codenv use codex primary | source
|
|
|
216
216
|
"type": "command",
|
|
217
217
|
"padding": 0
|
|
218
218
|
},
|
|
219
|
+
"pricing": {
|
|
220
|
+
"models": {
|
|
221
|
+
"Claude Sonnet 4.5": {
|
|
222
|
+
"input": 3.0,
|
|
223
|
+
"output": 15.0,
|
|
224
|
+
"cacheWrite": 3.75,
|
|
225
|
+
"cacheRead": 0.3,
|
|
226
|
+
"description": "平衡性能与速度,适合日常使用"
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
},
|
|
219
230
|
"profiles": {
|
|
220
231
|
"p_a1b2c3": {
|
|
221
232
|
"name": "primary",
|
|
@@ -246,10 +257,16 @@ codenv use codex primary | source
|
|
|
246
257
|
- `type`:字符串;statusLine 类型(默认 `command`)。
|
|
247
258
|
- `padding`:数字;statusLine padding(默认 0)。
|
|
248
259
|
- `settingsPath`:可选;覆盖 `~/.claude/settings.json`(也可用 `CODE_ENV_CLAUDE_SETTINGS_PATH`)。
|
|
260
|
+
- `pricing`:可选;模型价格(美元 / 1M tokens),用于在状态栏将 token 用量换算为美元额度。
|
|
261
|
+
- `models`:模型名称到价格的映射(匹配时忽略大小写与格式差异)。
|
|
262
|
+
- `input`/`output`/`cacheRead`/`cacheWrite`:输入/输出/缓存价格。
|
|
263
|
+
- 优先使用 profile 中指定的 `model`,否则使用状态栏输入的模型名称;无拆分则不显示金额。
|
|
249
264
|
- `name`:用于展示的 profile 名称,`codenv list` 与 `codenv use <name>` 会使用它。
|
|
250
265
|
- `type`:可选,`codex` 或 `claude`(别名 `cc`),便于用 `codenv use <type> <name>` 匹配。
|
|
251
266
|
- `note`:显示在 `codenv list` 输出中。
|
|
252
267
|
- `removeFiles`:可选;`codenv use` 会输出对应 `rm -f`。Codex profile 还会删除 `~/.codex/auth.json`。
|
|
268
|
+
- `pricing`(profile 内):可选;为单个 profile 覆盖价格。支持 `model` 以及 `input`/`output`/`cacheRead`/`cacheWrite`。
|
|
269
|
+
- `multiplier`:可选;倍率(数字)。
|
|
253
270
|
- `ANTHROPIC_AUTH_TOKEN`:当设置了 `ANTHROPIC_API_KEY` 时,`codenv use` 会自动以同样的值导出 `ANTHROPIC_AUTH_TOKEN`。
|
|
254
271
|
- `commands`:可选;原样输出到切换脚本中。
|
|
255
272
|
|
package/bin/commands/list.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.printList = printList;
|
|
|
4
4
|
const display_1 = require("../profile/display");
|
|
5
5
|
const defaults_1 = require("../config/defaults");
|
|
6
6
|
const usage_1 = require("../usage");
|
|
7
|
+
const pricing_1 = require("../usage/pricing");
|
|
7
8
|
function printList(config, configPath) {
|
|
8
9
|
const rows = (0, display_1.buildListRows)(config, defaults_1.getResolvedDefaultProfileKeys);
|
|
9
10
|
if (rows.length === 0) {
|
|
@@ -12,6 +13,7 @@ function printList(config, configPath) {
|
|
|
12
13
|
}
|
|
13
14
|
try {
|
|
14
15
|
const usageTotals = (0, usage_1.readUsageTotalsIndex)(config, configPath, true);
|
|
16
|
+
const usageCosts = (0, usage_1.readUsageCostIndex)(config, configPath, false);
|
|
15
17
|
if (usageTotals) {
|
|
16
18
|
for (const row of rows) {
|
|
17
19
|
if (!row.usageType)
|
|
@@ -23,6 +25,19 @@ function printList(config, configPath) {
|
|
|
23
25
|
row.totalTokens = usage.total;
|
|
24
26
|
}
|
|
25
27
|
}
|
|
28
|
+
if (usageCosts) {
|
|
29
|
+
for (const row of rows) {
|
|
30
|
+
if (!row.usageType)
|
|
31
|
+
continue;
|
|
32
|
+
const cost = (0, usage_1.resolveUsageCostForProfile)(usageCosts, row.usageType, row.key, row.name);
|
|
33
|
+
if (!cost)
|
|
34
|
+
continue;
|
|
35
|
+
row.todayCost = cost.today;
|
|
36
|
+
row.totalCost = cost.total;
|
|
37
|
+
row.todayBilledTokens = cost.todayTokens;
|
|
38
|
+
row.totalBilledTokens = cost.totalTokens;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
26
41
|
}
|
|
27
42
|
catch {
|
|
28
43
|
// ignore usage sync errors
|
|
@@ -32,8 +47,14 @@ function printList(config, configPath) {
|
|
|
32
47
|
const headerToday = "TODAY";
|
|
33
48
|
const headerTotal = "TOTAL";
|
|
34
49
|
const headerNote = "NOTE";
|
|
35
|
-
const todayTexts = rows.map((row) =>
|
|
36
|
-
|
|
50
|
+
const todayTexts = rows.map((row) => {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
return formatUsageWithCost(row.todayTokens, (_a = row.todayBilledTokens) !== null && _a !== void 0 ? _a : null, (_b = row.todayCost) !== null && _b !== void 0 ? _b : null);
|
|
53
|
+
});
|
|
54
|
+
const totalTexts = rows.map((row) => {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
return formatUsageWithCost(row.totalTokens, (_a = row.totalBilledTokens) !== null && _a !== void 0 ? _a : null, (_b = row.totalCost) !== null && _b !== void 0 ? _b : null);
|
|
57
|
+
});
|
|
37
58
|
const nameWidth = Math.max(headerName.length, ...rows.map((row) => row.name.length));
|
|
38
59
|
const typeWidth = Math.max(headerType.length, ...rows.map((row) => row.type.length));
|
|
39
60
|
const todayWidth = Math.max(headerToday.length, ...todayTexts.map((v) => v.length));
|
|
@@ -55,3 +76,24 @@ function printList(config, configPath) {
|
|
|
55
76
|
}
|
|
56
77
|
}
|
|
57
78
|
}
|
|
79
|
+
function formatUsageWithCost(tokens, billedTokens, cost) {
|
|
80
|
+
const tokenText = (0, usage_1.formatTokenCount)(tokens !== null && tokens !== void 0 ? tokens : null);
|
|
81
|
+
if (tokenText === "-")
|
|
82
|
+
return tokenText;
|
|
83
|
+
if (cost === null || !Number.isFinite(cost))
|
|
84
|
+
return tokenText;
|
|
85
|
+
if (tokens === null || tokens === undefined || !Number.isFinite(tokens)) {
|
|
86
|
+
return tokenText;
|
|
87
|
+
}
|
|
88
|
+
if (billedTokens === null || !Number.isFinite(billedTokens)) {
|
|
89
|
+
return `${tokenText} (${(0, pricing_1.formatUsdAmount)(cost)})`;
|
|
90
|
+
}
|
|
91
|
+
if (billedTokens >= tokens) {
|
|
92
|
+
return `${tokenText} (${(0, pricing_1.formatUsdAmount)(cost)})`;
|
|
93
|
+
}
|
|
94
|
+
const billedText = (0, usage_1.formatTokenCount)(billedTokens);
|
|
95
|
+
if (billedText === "-") {
|
|
96
|
+
return `${tokenText} (${(0, pricing_1.formatUsdAmount)(cost)})`;
|
|
97
|
+
}
|
|
98
|
+
return `${tokenText} (billed ${billedText}, ${(0, pricing_1.formatUsdAmount)(cost)})`;
|
|
99
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.appendStatuslineDebug = appendStatuslineDebug;
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const os = require("os");
|
|
7
|
+
const utils_1 = require("../shell/utils");
|
|
8
|
+
function isStatuslineDebugEnabled() {
|
|
9
|
+
const raw = process.env.CODE_ENV_STATUSLINE_DEBUG;
|
|
10
|
+
if (!raw)
|
|
11
|
+
return false;
|
|
12
|
+
const value = String(raw).trim().toLowerCase();
|
|
13
|
+
if (!value)
|
|
14
|
+
return false;
|
|
15
|
+
return !["0", "false", "no", "off"].includes(value);
|
|
16
|
+
}
|
|
17
|
+
function resolveDefaultConfigDir(configPath) {
|
|
18
|
+
if (configPath)
|
|
19
|
+
return path.dirname(configPath);
|
|
20
|
+
return path.join(os.homedir(), ".config", "code-env");
|
|
21
|
+
}
|
|
22
|
+
function getStatuslineDebugPath(configPath) {
|
|
23
|
+
const envPath = (0, utils_1.resolvePath)(process.env.CODE_ENV_STATUSLINE_DEBUG_PATH);
|
|
24
|
+
if (envPath)
|
|
25
|
+
return envPath;
|
|
26
|
+
return path.join(resolveDefaultConfigDir(configPath), "statusline-debug.jsonl");
|
|
27
|
+
}
|
|
28
|
+
function appendStatuslineDebug(configPath, payload) {
|
|
29
|
+
if (!isStatuslineDebugEnabled())
|
|
30
|
+
return;
|
|
31
|
+
try {
|
|
32
|
+
const debugPath = getStatuslineDebugPath(configPath);
|
|
33
|
+
const dir = path.dirname(debugPath);
|
|
34
|
+
if (!fs.existsSync(dir)) {
|
|
35
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
fs.appendFileSync(debugPath, `${JSON.stringify(payload)}\n`, "utf8");
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// ignore debug logging failures
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCwdSegment = getCwdSegment;
|
|
4
|
+
exports.formatUsageSegment = formatUsageSegment;
|
|
5
|
+
exports.formatModelSegment = formatModelSegment;
|
|
6
|
+
exports.formatProfileSegment = formatProfileSegment;
|
|
7
|
+
exports.formatContextSegment = formatContextSegment;
|
|
8
|
+
exports.formatContextUsedSegment = formatContextUsedSegment;
|
|
9
|
+
exports.formatModeSegment = formatModeSegment;
|
|
10
|
+
const usage_1 = require("../usage");
|
|
11
|
+
const pricing_1 = require("../usage/pricing");
|
|
12
|
+
const style_1 = require("./style");
|
|
13
|
+
const path = require("path");
|
|
14
|
+
function getCwdSegment(cwd) {
|
|
15
|
+
if (!cwd)
|
|
16
|
+
return null;
|
|
17
|
+
const base = path.basename(cwd) || cwd;
|
|
18
|
+
const segment = `${style_1.ICON_CWD} ${base}`;
|
|
19
|
+
return (0, style_1.dim)(segment);
|
|
20
|
+
}
|
|
21
|
+
function formatUsageSegment(todayCost, sessionCost) {
|
|
22
|
+
if (todayCost === null && sessionCost === null)
|
|
23
|
+
return null;
|
|
24
|
+
const todayText = `T ${(0, pricing_1.formatUsdAmount)(todayCost)}`;
|
|
25
|
+
const sessionText = `S ${(0, pricing_1.formatUsdAmount)(sessionCost)}`;
|
|
26
|
+
const text = `${todayText} / ${sessionText}`;
|
|
27
|
+
return (0, style_1.colorize)(`${style_1.ICON_USAGE} ${text}`, "33");
|
|
28
|
+
}
|
|
29
|
+
function formatModelSegment(model, provider) {
|
|
30
|
+
if (!model)
|
|
31
|
+
return null;
|
|
32
|
+
const providerLabel = provider ? `${provider}:${model}` : model;
|
|
33
|
+
return (0, style_1.colorize)(`${style_1.ICON_MODEL} ${providerLabel}`, "35");
|
|
34
|
+
}
|
|
35
|
+
function formatProfileSegment(type, profileKey, profileName) {
|
|
36
|
+
const name = profileName || profileKey;
|
|
37
|
+
if (!name)
|
|
38
|
+
return null;
|
|
39
|
+
const label = type ? `${type}:${name}` : name;
|
|
40
|
+
return (0, style_1.colorize)(`${style_1.ICON_PROFILE} ${label}`, "37");
|
|
41
|
+
}
|
|
42
|
+
function formatContextSegment(contextLeft) {
|
|
43
|
+
if (contextLeft === null)
|
|
44
|
+
return null;
|
|
45
|
+
const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
|
|
46
|
+
return (0, style_1.colorize)(`${style_1.ICON_CONTEXT} ${left}% left`, "36");
|
|
47
|
+
}
|
|
48
|
+
function formatContextUsedSegment(usedTokens) {
|
|
49
|
+
if (usedTokens === null)
|
|
50
|
+
return null;
|
|
51
|
+
return (0, style_1.colorize)(`${style_1.ICON_CONTEXT} ${(0, usage_1.formatTokenCount)(usedTokens)} used`, "36");
|
|
52
|
+
}
|
|
53
|
+
function formatModeSegment(reviewMode) {
|
|
54
|
+
if (!reviewMode)
|
|
55
|
+
return null;
|
|
56
|
+
return (0, style_1.colorize)(`${style_1.ICON_REVIEW} review`, "34");
|
|
57
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGitStatus = getGitStatus;
|
|
4
|
+
exports.formatGitSegment = formatGitSegment;
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
|
+
const style_1 = require("./style");
|
|
7
|
+
const style_2 = require("./style");
|
|
8
|
+
function getGitStatus(cwd) {
|
|
9
|
+
if (!cwd)
|
|
10
|
+
return null;
|
|
11
|
+
const result = (0, child_process_1.spawnSync)("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
|
|
12
|
+
encoding: "utf8",
|
|
13
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
14
|
+
});
|
|
15
|
+
if (result.status !== 0 || !result.stdout)
|
|
16
|
+
return null;
|
|
17
|
+
const status = {
|
|
18
|
+
branch: null,
|
|
19
|
+
ahead: 0,
|
|
20
|
+
behind: 0,
|
|
21
|
+
staged: 0,
|
|
22
|
+
unstaged: 0,
|
|
23
|
+
untracked: 0,
|
|
24
|
+
conflicted: 0,
|
|
25
|
+
};
|
|
26
|
+
const lines = result.stdout.split(/\r?\n/);
|
|
27
|
+
for (const line of lines) {
|
|
28
|
+
if (!line)
|
|
29
|
+
continue;
|
|
30
|
+
if (line.startsWith("# branch.head ")) {
|
|
31
|
+
status.branch = line.slice("# branch.head ".length).trim();
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (line.startsWith("# branch.ab ")) {
|
|
35
|
+
const parts = line
|
|
36
|
+
.slice("# branch.ab ".length)
|
|
37
|
+
.trim()
|
|
38
|
+
.split(/\s+/);
|
|
39
|
+
for (const part of parts) {
|
|
40
|
+
if (part.startsWith("+"))
|
|
41
|
+
status.ahead = Number(part.slice(1)) || 0;
|
|
42
|
+
if (part.startsWith("-"))
|
|
43
|
+
status.behind = Number(part.slice(1)) || 0;
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (line.startsWith("? ")) {
|
|
48
|
+
status.untracked += 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (line.startsWith("u ")) {
|
|
52
|
+
status.conflicted += 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (line.startsWith("1 ") || line.startsWith("2 ")) {
|
|
56
|
+
const parts = line.split(/\s+/);
|
|
57
|
+
const xy = parts[1] || "";
|
|
58
|
+
const staged = xy[0];
|
|
59
|
+
const unstaged = xy[1];
|
|
60
|
+
if (staged && staged !== ".")
|
|
61
|
+
status.staged += 1;
|
|
62
|
+
if (unstaged && unstaged !== ".")
|
|
63
|
+
status.unstaged += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!status.branch) {
|
|
68
|
+
status.branch = "HEAD";
|
|
69
|
+
}
|
|
70
|
+
return status;
|
|
71
|
+
}
|
|
72
|
+
function formatGitSegment(status) {
|
|
73
|
+
if (!status || !status.branch)
|
|
74
|
+
return null;
|
|
75
|
+
const meta = [];
|
|
76
|
+
const dirtyCount = status.staged + status.unstaged + status.untracked;
|
|
77
|
+
if (status.ahead > 0)
|
|
78
|
+
meta.push(`↑${status.ahead}`);
|
|
79
|
+
if (status.behind > 0)
|
|
80
|
+
meta.push(`↓${status.behind}`);
|
|
81
|
+
if (status.conflicted > 0)
|
|
82
|
+
meta.push(`✖${status.conflicted}`);
|
|
83
|
+
if (dirtyCount > 0)
|
|
84
|
+
meta.push(`+${dirtyCount}`);
|
|
85
|
+
const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
|
|
86
|
+
const text = `${style_2.ICON_GIT} ${status.branch}${suffix}`;
|
|
87
|
+
const hasConflicts = status.conflicted > 0;
|
|
88
|
+
const isDirty = dirtyCount > 0;
|
|
89
|
+
if (hasConflicts)
|
|
90
|
+
return (0, style_1.colorize)(text, "31");
|
|
91
|
+
if (isDirty)
|
|
92
|
+
return (0, style_1.colorize)(text, "33");
|
|
93
|
+
if (status.ahead > 0 || status.behind > 0)
|
|
94
|
+
return (0, style_1.colorize)(text, "36");
|
|
95
|
+
return (0, style_1.colorize)(text, "32");
|
|
96
|
+
}
|