cc-pets 1.2.8 → 1.3.0
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 +5 -1
- package/Sources/CCPets/CCPetsAppDelegate.h +3 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +96 -2
- package/Sources/CCPets/CCPetsPaths.h +17 -0
- package/Sources/CCPets/CCPetsPaths.m +50 -0
- package/Sources/CCPets/CCPetsPreviews.m +7 -0
- package/Sources/CCPets/CCPetsUsage.h +5 -0
- package/Sources/CCPets/CCPetsUsage.m +435 -18
- package/Sources/CCPets/CCPetsUsageMonitor.m +36 -7
- package/Sources/CCPets/MenuToggleSwitch.h +4 -0
- package/Sources/CCPets/MenuToggleSwitch.m +48 -0
- package/Sources/CCPets/PetView.m +58 -5
- package/Sources/CCPets/QuotaDashboardView.h +10 -0
- package/Sources/CCPets/QuotaDashboardView.m +369 -78
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +1 -1
- package/scripts/detect-cli.mjs +68 -0
- package/scripts/install-claude-hooks.mjs +64 -17
- package/scripts/install-codex-hooks.mjs +10 -0
- package/scripts/uninstall-integrations.mjs +15 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// "这台机器上到底装没装 Codex / Claude Code CLI"。安装器据此决定是否写它们的配置:
|
|
2
|
+
// 以前无条件 mkdir + 写 hooks.json / settings.json,没装的用户会被凭空造出 ~/.codex、
|
|
3
|
+
// ~/.claude,甚至被接管一个他还没有的 status line。
|
|
4
|
+
//
|
|
5
|
+
// 判据是"配置目录已存在"或"PATH 里有真二进制",两者取或:
|
|
6
|
+
// - 装了但从没跑过 → 没有配置目录,靠 PATH 认出来;
|
|
7
|
+
// - 装在非标准位置或用别的启动方式 → PATH 里找不到,靠配置目录认出来。
|
|
8
|
+
// 安装器自己不再创建这两个目录,所以"目录存在"是一个干净的信号,不会自证。
|
|
9
|
+
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
|
|
14
|
+
const shimDirectory = () => path.resolve(
|
|
15
|
+
process.env.CC_PETS_SHIM_DIR || path.join(os.homedir(), ".cc-pets/shims"));
|
|
16
|
+
|
|
17
|
+
const isDirectory = (target) => {
|
|
18
|
+
try {
|
|
19
|
+
return fs.statSync(target).isDirectory();
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// 不能直接用 `which codex`:安装完成后 shim 目录就排在 PATH 最前面,命中的是我们自己的
|
|
26
|
+
// 包装器,等于自己证明自己。这里跳过 shim 目录,并且对解析后落在包装器上的候选再兜一层
|
|
27
|
+
// (复制而非软链安装时,shim 可能不在 CC_PETS_SHIM_DIR 里)。
|
|
28
|
+
const hasRealExecutable = (name) => {
|
|
29
|
+
const shim = shimDirectory();
|
|
30
|
+
const wrapperNames = new Set([`${name}-with-pet`, "codex-with-pet", "claude-with-pet"]);
|
|
31
|
+
for (const entry of (process.env.PATH || "").split(path.delimiter)) {
|
|
32
|
+
if (!entry) continue;
|
|
33
|
+
let resolvedEntry;
|
|
34
|
+
try {
|
|
35
|
+
resolvedEntry = fs.realpathSync(entry);
|
|
36
|
+
} catch {
|
|
37
|
+
resolvedEntry = path.resolve(entry);
|
|
38
|
+
}
|
|
39
|
+
if (resolvedEntry === shim) continue;
|
|
40
|
+
const candidate = path.join(entry, name);
|
|
41
|
+
try {
|
|
42
|
+
const stats = fs.statSync(candidate);
|
|
43
|
+
if (!stats.isFile() || (stats.mode & 0o111) === 0) continue;
|
|
44
|
+
const real = fs.realpathSync(candidate);
|
|
45
|
+
if (path.dirname(real) === shim) continue;
|
|
46
|
+
if (wrapperNames.has(path.basename(real))) continue;
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
// 断链或没权限的候选直接跳过,继续看 PATH 里的下一个。
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// 逃生舱:探测漏判时(装在容器里、PATH 被裁剪过、CI 造假环境)用户可以强制安装。
|
|
56
|
+
// CODEX_REAL_BIN / CLAUDE_REAL_BIN 沿用 bin/*-with-pet 已有的约定。
|
|
57
|
+
const forced = (realBinVariable) =>
|
|
58
|
+
process.env.CC_PETS_ASSUME_CLI === "1" || (process.env[realBinVariable] || "").length > 0;
|
|
59
|
+
|
|
60
|
+
export const detectCodexCLI = () =>
|
|
61
|
+
forced("CODEX_REAL_BIN") ||
|
|
62
|
+
isDirectory(process.env.CODEX_HOME || path.join(os.homedir(), ".codex")) ||
|
|
63
|
+
hasRealExecutable("codex");
|
|
64
|
+
|
|
65
|
+
export const detectClaudeCLI = () =>
|
|
66
|
+
forced("CLAUDE_REAL_BIN") ||
|
|
67
|
+
isDirectory(process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude")) ||
|
|
68
|
+
hasRealExecutable("claude");
|
|
@@ -4,6 +4,7 @@ import fs from "node:fs";
|
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { detectClaudeCLI } from "./detect-cli.mjs";
|
|
7
8
|
|
|
8
9
|
const binary = process.argv[2];
|
|
9
10
|
if (!binary) {
|
|
@@ -11,6 +12,15 @@ if (!binary) {
|
|
|
11
12
|
process.exit(2);
|
|
12
13
|
}
|
|
13
14
|
|
|
15
|
+
// 没装 Claude Code 就什么都不写。这里尤其重要:下面的 createStatusLine 分支会创建
|
|
16
|
+
// ~/.claude/statusline-command.sh 并把 settings.json 的 statusLine 指过去,
|
|
17
|
+
// 对还没装 Claude 的用户等于提前替他决定了状态栏。exit 0 的理由见 install-codex-hooks.mjs。
|
|
18
|
+
if (!detectClaudeCLI()) {
|
|
19
|
+
console.log("未检测到 Claude Code CLI,跳过 Claude Hooks 与 status line 接入。");
|
|
20
|
+
console.log("之后安装了 Claude Code,执行 cc-pets install 即可补装。");
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
|
|
14
24
|
const claudeHome = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude");
|
|
15
25
|
const settingsPath = path.join(claudeHome, "settings.json");
|
|
16
26
|
const marker = "CC_PETS_CLAUDE_AGENT_HOOK=1";
|
|
@@ -41,15 +51,35 @@ const removeStatusLineBlock = (value) => value
|
|
|
41
51
|
.replace(statusLineBlockPattern, "")
|
|
42
52
|
.replace(/^\n+/, "")
|
|
43
53
|
.replace(/\n{3,}/g, "\n\n");
|
|
44
|
-
|
|
54
|
+
// `bash ~/.claude/statusline-command.sh` 指向的仍是一个能就地注入的脚本,只是多了解释器
|
|
55
|
+
// 前缀和 `~/`。不剥掉就会被下面的"绝对路径 + 无空格"两条拒掉,白白退化成 base64 包装器,
|
|
56
|
+
// 而包装器形态对改 settings.json 的 agent 是黑盒(读不出原命令,容易整条覆盖掉)。
|
|
57
|
+
// 带 -c 的形式除外:`-c` 后面是命令串而非脚本路径,不能当路径注入。
|
|
58
|
+
const interpreterPattern =
|
|
59
|
+
/^(?:\/usr\/bin\/env\s+)?(?:[^\s'"]*\/)?(?:ba|z|k|da)?sh((?:\s+-[A-Za-z]+)*)\s+(.+)$/s;
|
|
60
|
+
const unquote = (value) => {
|
|
61
|
+
const quoted = value.match(/^(['"])(.*)\1$/s);
|
|
62
|
+
return quoted ? quoted[2] : value;
|
|
63
|
+
};
|
|
64
|
+
// target 保留用户原来的写法(含 `~/`),script 才是展开后用于读写文件的绝对路径。
|
|
65
|
+
// 官方形态就是 `~/.claude/statusline.sh`,`~` 是刻意的可移植写法,不能展开后写回配置。
|
|
66
|
+
const parseStatusLineCommand = (value) => {
|
|
45
67
|
if (typeof value !== "string") return null;
|
|
46
|
-
let candidate = value.trim();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
68
|
+
let candidate = unquote(value.trim());
|
|
69
|
+
let hasInterpreter = false;
|
|
70
|
+
let flags = "";
|
|
71
|
+
const interpreted = candidate.match(interpreterPattern);
|
|
72
|
+
if (interpreted && !/-[A-Za-z]*c(?=\s|$)/.test(interpreted[1])) {
|
|
73
|
+
hasInterpreter = true;
|
|
74
|
+
flags = interpreted[1].trim();
|
|
75
|
+
candidate = unquote(interpreted[2].trim());
|
|
76
|
+
}
|
|
77
|
+
let resolved = candidate;
|
|
78
|
+
if (resolved.startsWith("~/")) resolved = path.join(os.homedir(), resolved.slice(2));
|
|
79
|
+
if (!path.isAbsolute(resolved) || /[\s;&|<>`$()]/.test(resolved)) return null;
|
|
80
|
+
return { script: path.resolve(resolved), target: candidate, hasInterpreter, flags };
|
|
52
81
|
};
|
|
82
|
+
const resolveStatusLineScript = (value) => parseStatusLineCommand(value)?.script ?? null;
|
|
53
83
|
const statusLinePrelude = [
|
|
54
84
|
statusLineStartMarker,
|
|
55
85
|
`CC_PETS_STATUSLINE_INPUT="$(/usr/bin/mktemp "\${TMPDIR:-/tmp}/cc-pets-statusline.XXXXXX")" || exit 1`,
|
|
@@ -67,14 +97,14 @@ const installStatusLinePrelude = (scriptPath, createIfMissing = false) => {
|
|
|
67
97
|
let mode = 0o700;
|
|
68
98
|
if (fs.existsSync(scriptPath)) {
|
|
69
99
|
const stats = fs.statSync(scriptPath);
|
|
70
|
-
if (!stats.isFile()) return false;
|
|
100
|
+
if (!stats.isFile()) return { installed: false, hadShebang: false };
|
|
71
101
|
original = fs.readFileSync(scriptPath, "utf8");
|
|
72
102
|
mode = stats.mode;
|
|
73
103
|
} else if (createIfMissing) {
|
|
74
104
|
fs.mkdirSync(path.dirname(scriptPath), { recursive: true });
|
|
75
105
|
original = `#!/bin/zsh\n${createdStatusLineMarker}\n`;
|
|
76
106
|
} else {
|
|
77
|
-
return false;
|
|
107
|
+
return { installed: false, hadShebang: false };
|
|
78
108
|
}
|
|
79
109
|
|
|
80
110
|
const cleaned = removeStatusLineBlock(original);
|
|
@@ -91,7 +121,7 @@ const installStatusLinePrelude = (scriptPath, createIfMissing = false) => {
|
|
|
91
121
|
fs.writeFileSync(temporaryPath, updated, { mode });
|
|
92
122
|
fs.renameSync(temporaryPath, scriptPath);
|
|
93
123
|
fs.chmodSync(scriptPath, mode | 0o100);
|
|
94
|
-
return true;
|
|
124
|
+
return { installed: true, hadShebang: hasShebang };
|
|
95
125
|
};
|
|
96
126
|
const command = `${marker} ${shellQuote(path.resolve(binary))} --hook`;
|
|
97
127
|
const events = [
|
|
@@ -162,14 +192,31 @@ if (!originalStatusLineCommand) {
|
|
|
162
192
|
}
|
|
163
193
|
config.statusLine = { ...config.statusLine, type: "command", command: originalStatusLineCommand };
|
|
164
194
|
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
)
|
|
195
|
+
const parsedStatusLine = parseStatusLineCommand(originalStatusLineCommand);
|
|
196
|
+
const statusLineScript = parsedStatusLine?.script ?? null;
|
|
197
|
+
const preludeResult = statusLineScript
|
|
198
|
+
? installStatusLinePrelude(statusLineScript, createdStatusLine)
|
|
199
|
+
: { installed: false, hadShebang: false };
|
|
200
|
+
const statusLineInstalled = preludeResult.installed;
|
|
201
|
+
|
|
202
|
+
// 只在原命令带解释器前缀(`bash ~/x.sh`)时才改写 command,去掉那层前缀让 agent 能直接读懂。
|
|
203
|
+
// 纯路径形态——包括官方默认的 `~/.claude/statusline.sh`——一个字都不动,写法原样保留。
|
|
204
|
+
// 两条额外的闸门,都是为了不改变脚本的执行语义:
|
|
205
|
+
// * 带 flag(`sh -e ~/x.sh`)不动,否则 `-e` 这类选项会被悄悄丢掉;
|
|
206
|
+
// * 脚本原本没有 shebang 的不动,否则 prelude 补的 `#!/bin/zsh` 会把本来用 bash 跑的
|
|
207
|
+
// 脚本换成 zsh 跑。用户写 `bash ~/x.sh` 往往正是因为脚本没有 shebang。
|
|
208
|
+
if (
|
|
209
|
+
statusLineInstalled &&
|
|
210
|
+
parsedStatusLine?.hasInterpreter &&
|
|
211
|
+
!parsedStatusLine.flags &&
|
|
212
|
+
preludeResult.hadShebang
|
|
213
|
+
) {
|
|
214
|
+
config.statusLine = { ...config.statusLine, command: parsedStatusLine.target };
|
|
215
|
+
}
|
|
169
216
|
|
|
170
|
-
// 就地往用户脚本里插 prelude 只对"
|
|
171
|
-
// 形态(`bun x ccstatusline`、`npx
|
|
172
|
-
// 一律拒绝。以前这里只打印一行警告就算了,而 statusline 是 Claude 额度的唯一来源
|
|
217
|
+
// 就地往用户脚本里插 prelude 只对"能解析出脚本路径的命令"成立。真·第三方状态栏不是这个
|
|
218
|
+
// 形态(`bun x ccstatusline`、`npx …`、任何带管道或 `sh -c` 的命令行),
|
|
219
|
+
// resolveStatusLineScript 一律拒绝。以前这里只打印一行警告就算了,而 statusline 是 Claude 额度的唯一来源
|
|
173
220
|
// (转录 jsonl 和 stats-cache.json 里都没有 rate_limits),于是这类用户的额度永远是 `--`。
|
|
174
221
|
// 回退到包装器:command 换成 claude-statusline-with-pet + base64 编码的原命令,它先把 stdin
|
|
175
222
|
// 喂给 cc-pets --claude-usage,再原样转发给原命令执行。卸载侧本来就认这个形态(marker +
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
|
+
import { detectCodexCLI } from "./detect-cli.mjs";
|
|
6
7
|
|
|
7
8
|
const binary = process.argv[2];
|
|
8
9
|
if (!binary) {
|
|
@@ -10,6 +11,15 @@ if (!binary) {
|
|
|
10
11
|
process.exit(2);
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
// 没装 Codex 就什么都不写。必须 exit 0:install-shell-integration.sh 是 set -eu,
|
|
15
|
+
// 这里非零退出会把后面的 Claude Hooks、更新器、shim 和 .zshrc 全部带停,
|
|
16
|
+
// 用户会拿到一个装了一半的状态。
|
|
17
|
+
if (!detectCodexCLI()) {
|
|
18
|
+
console.log("未检测到 Codex CLI,跳过 Codex Hooks 安装。");
|
|
19
|
+
console.log("之后安装了 Codex,执行 cc-pets install 即可补装。");
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
14
24
|
const hooksPath = path.join(codexHome, "hooks.json");
|
|
15
25
|
const marker = "CC_PETS_CODEX_AGENT_HOOK=1";
|
|
@@ -31,11 +31,23 @@ function isManagedStatusLine(value, marker) {
|
|
|
31
31
|
/\/bin\/claude-statusline-with-pet' '[A-Za-z0-9+/=]*'$/.test(value);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// 与 install-claude-hooks.mjs 保持一致:`bash ~/x.sh` 这类带解释器前缀的命令同样指向一个
|
|
35
|
+
// 被就地注入过的脚本,卸载时也要能解析出来,否则 prelude 会残留在脚本里。
|
|
36
|
+
const interpreterPattern =
|
|
37
|
+
/^(?:\/usr\/bin\/env\s+)?(?:[^\s'"]*\/)?(?:ba|z|k|da)?sh((?:\s+-[A-Za-z]+)*)\s+(.+)$/s;
|
|
38
|
+
|
|
39
|
+
function unquote(value) {
|
|
40
|
+
const quoted = value.match(/^(['"])(.*)\1$/s);
|
|
41
|
+
return quoted ? quoted[2] : value;
|
|
42
|
+
}
|
|
43
|
+
|
|
34
44
|
function resolveStatusLineScript(value) {
|
|
35
45
|
if (typeof value !== "string") return null;
|
|
36
|
-
let candidate = value.trim();
|
|
37
|
-
const
|
|
38
|
-
if (
|
|
46
|
+
let candidate = unquote(value.trim());
|
|
47
|
+
const interpreted = candidate.match(interpreterPattern);
|
|
48
|
+
if (interpreted && !/-[A-Za-z]*c(?=\s|$)/.test(interpreted[1])) {
|
|
49
|
+
candidate = unquote(interpreted[2].trim());
|
|
50
|
+
}
|
|
39
51
|
if (candidate.startsWith("~/")) candidate = path.join(os.homedir(), candidate.slice(2));
|
|
40
52
|
if (!path.isAbsolute(candidate) || /[\s;&|<>`$()]/.test(candidate)) return null;
|
|
41
53
|
return path.resolve(candidate);
|