cc-pets 1.2.1 → 1.2.2
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
CHANGED
|
@@ -38,8 +38,9 @@
|
|
|
38
38
|
Agent 动画分别由 Codex Hooks 和 Claude Code Hooks 驱动。首次安装或 Codex Hook 内容更新后,启动 Codex 并执行
|
|
39
39
|
`/hooks`,检查并信任 CC Pets Hook;未经信任的用户级 Hook 会被 Codex 安全机制跳过。
|
|
40
40
|
Claude Code Hook 会合并到 `~/.claude/settings.json`,现有的 `env`、`model`、`statusLine`
|
|
41
|
-
以及其他 Hooks
|
|
42
|
-
|
|
41
|
+
以及其他 Hooks 都会保留。额度采集块会注入现有 status line 脚本,但配置中的 `command`
|
|
42
|
+
仍保持原脚本路径,Claude Code 可以继续查找和修改该脚本;脚本输入会同时提供给原状态栏
|
|
43
|
+
和桌宠,原有输出不变。
|
|
43
44
|
通过 npm 安装或升级时会自动重新构建应用、安装或更新两套 Hooks,并配置 shell 集成;如果旧版桌宠正在运行,
|
|
44
45
|
安装器会自动重启桌宠,使新版本生效。`cc-pets install` 保留为手动修复和重新初始化命令。
|
|
45
46
|
|
package/package.json
CHANGED
|
@@ -5,9 +5,8 @@ import os from "node:os";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
|
|
7
7
|
const binary = process.argv[2];
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
console.error("用法: install-claude-hooks.mjs /absolute/path/to/cc-pets /absolute/path/to/claude-statusline-with-pet");
|
|
8
|
+
if (!binary) {
|
|
9
|
+
console.error("用法: install-claude-hooks.mjs /absolute/path/to/cc-pets");
|
|
11
10
|
process.exit(2);
|
|
12
11
|
}
|
|
13
12
|
|
|
@@ -20,6 +19,9 @@ const managedHookSignatures = [
|
|
|
20
19
|
{ marker: "CLAUDE_PET_AGENT_HOOK=1", executable: "codex-pet" }
|
|
21
20
|
];
|
|
22
21
|
const managedStatusLineMarkers = [statusLineMarker, "CLAUDE_PET_STATUS_LINE=1"];
|
|
22
|
+
const statusLineStartMarker = "# >>> cc-pets-statusline >>>";
|
|
23
|
+
const statusLineEndMarker = "# <<< cc-pets-statusline <<<";
|
|
24
|
+
const createdStatusLineMarker = "# CC Pets created this status line script";
|
|
23
25
|
const isManagedHookCommand = (value, signature) =>
|
|
24
26
|
typeof value === "string" &&
|
|
25
27
|
value.startsWith(`${signature.marker} `) &&
|
|
@@ -29,6 +31,67 @@ const isManagedStatusLine = (value, candidateMarker) =>
|
|
|
29
31
|
value.startsWith(`${candidateMarker} `) &&
|
|
30
32
|
/\/bin\/claude-statusline-with-pet' '[A-Za-z0-9+/=]*'$/.test(value);
|
|
31
33
|
const shellQuote = (value) => `'${value.replaceAll("'", `'\\''`)}'`;
|
|
34
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
35
|
+
const statusLineBlockPattern = new RegExp(
|
|
36
|
+
`(?:^|\\n)${escapeRegExp(statusLineStartMarker)}\\n[\\s\\S]*?\\n${escapeRegExp(statusLineEndMarker)}(?=\\n|$)`,
|
|
37
|
+
"g"
|
|
38
|
+
);
|
|
39
|
+
const removeStatusLineBlock = (value) => value
|
|
40
|
+
.replace(statusLineBlockPattern, "")
|
|
41
|
+
.replace(/^\n+/, "")
|
|
42
|
+
.replace(/\n{3,}/g, "\n\n");
|
|
43
|
+
const resolveStatusLineScript = (value) => {
|
|
44
|
+
if (typeof value !== "string") return null;
|
|
45
|
+
let candidate = value.trim();
|
|
46
|
+
const quoted = candidate.match(/^(['"])(.*)\1$/s);
|
|
47
|
+
if (quoted) candidate = quoted[2];
|
|
48
|
+
if (candidate.startsWith("~/")) candidate = path.join(os.homedir(), candidate.slice(2));
|
|
49
|
+
if (!path.isAbsolute(candidate) || /[\s;&|<>`$()]/.test(candidate)) return null;
|
|
50
|
+
return path.resolve(candidate);
|
|
51
|
+
};
|
|
52
|
+
const statusLinePrelude = [
|
|
53
|
+
statusLineStartMarker,
|
|
54
|
+
`CC_PETS_STATUSLINE_INPUT="$(/usr/bin/mktemp "\${TMPDIR:-/tmp}/cc-pets-statusline.XXXXXX")" || exit 1`,
|
|
55
|
+
`/bin/cat > "\${CC_PETS_STATUSLINE_INPUT}"`,
|
|
56
|
+
`if [ -x ${shellQuote(path.resolve(binary))} ]; then`,
|
|
57
|
+
` ${shellQuote(path.resolve(binary))} --claude-usage < "\${CC_PETS_STATUSLINE_INPUT}" >/dev/null 2>&1 || true`,
|
|
58
|
+
"fi",
|
|
59
|
+
`exec 0< "\${CC_PETS_STATUSLINE_INPUT}"`,
|
|
60
|
+
`/bin/rm -f "\${CC_PETS_STATUSLINE_INPUT}"`,
|
|
61
|
+
"unset CC_PETS_STATUSLINE_INPUT",
|
|
62
|
+
statusLineEndMarker
|
|
63
|
+
].join("\n");
|
|
64
|
+
const installStatusLinePrelude = (scriptPath, createIfMissing = false) => {
|
|
65
|
+
let original = "";
|
|
66
|
+
let mode = 0o700;
|
|
67
|
+
if (fs.existsSync(scriptPath)) {
|
|
68
|
+
const stats = fs.statSync(scriptPath);
|
|
69
|
+
if (!stats.isFile()) return false;
|
|
70
|
+
original = fs.readFileSync(scriptPath, "utf8");
|
|
71
|
+
mode = stats.mode;
|
|
72
|
+
} else if (createIfMissing) {
|
|
73
|
+
fs.mkdirSync(path.dirname(scriptPath), { recursive: true });
|
|
74
|
+
original = `#!/bin/zsh\n${createdStatusLineMarker}\n`;
|
|
75
|
+
} else {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const cleaned = removeStatusLineBlock(original);
|
|
80
|
+
const firstNewline = cleaned.indexOf("\n");
|
|
81
|
+
const hasShebang = cleaned.startsWith("#!");
|
|
82
|
+
const shebang = hasShebang
|
|
83
|
+
? cleaned.slice(0, firstNewline === -1 ? cleaned.length : firstNewline)
|
|
84
|
+
: "#!/bin/zsh";
|
|
85
|
+
const body = hasShebang
|
|
86
|
+
? (firstNewline === -1 ? "" : cleaned.slice(firstNewline + 1))
|
|
87
|
+
: cleaned;
|
|
88
|
+
const updated = `${shebang}\n${statusLinePrelude}\n${body}`.replace(/\n*$/, "\n");
|
|
89
|
+
const temporaryPath = `${scriptPath}.cc-pets.tmp`;
|
|
90
|
+
fs.writeFileSync(temporaryPath, updated, { mode });
|
|
91
|
+
fs.renameSync(temporaryPath, scriptPath);
|
|
92
|
+
fs.chmodSync(scriptPath, mode | 0o100);
|
|
93
|
+
return true;
|
|
94
|
+
};
|
|
32
95
|
const command = `${marker} ${shellQuote(path.resolve(binary))} --hook`;
|
|
33
96
|
const events = [
|
|
34
97
|
"SessionStart",
|
|
@@ -90,16 +153,28 @@ if (config.statusLine && typeof config.statusLine === "object" && !Array.isArray
|
|
|
90
153
|
} else {
|
|
91
154
|
config.statusLine = { type: "command" };
|
|
92
155
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
156
|
+
|
|
157
|
+
let createdStatusLine = false;
|
|
158
|
+
if (!originalStatusLineCommand) {
|
|
159
|
+
originalStatusLineCommand = path.join(claudeHome, "statusline-command.sh");
|
|
160
|
+
createdStatusLine = true;
|
|
161
|
+
}
|
|
162
|
+
config.statusLine = { ...config.statusLine, type: "command", command: originalStatusLineCommand };
|
|
163
|
+
|
|
164
|
+
const statusLineScript = resolveStatusLineScript(originalStatusLineCommand);
|
|
165
|
+
const statusLineInstalled = Boolean(
|
|
166
|
+
statusLineScript && installStatusLinePrelude(statusLineScript, createdStatusLine)
|
|
167
|
+
);
|
|
168
|
+
if (!statusLineInstalled) {
|
|
169
|
+
console.warn(`未修改 Claude status line:command 不是可直接编辑的脚本路径:${originalStatusLineCommand}`);
|
|
170
|
+
console.warn("Claude Hooks 仍会正常工作,但 CC Pets 暂时无法采集 Claude 额度。");
|
|
171
|
+
}
|
|
99
172
|
|
|
100
173
|
const temporaryPath = `${settingsPath}.cc-pets.tmp`;
|
|
101
174
|
fs.writeFileSync(temporaryPath, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
102
175
|
fs.renameSync(temporaryPath, settingsPath);
|
|
103
176
|
fs.chmodSync(settingsPath, 0o600);
|
|
104
177
|
console.log(`Claude Code Hooks 已安装到 ${settingsPath}`);
|
|
105
|
-
|
|
178
|
+
if (statusLineInstalled) {
|
|
179
|
+
console.log("Claude 额度采集已接入现有 status line。首次 API 响应后桌宠会显示额度。");
|
|
180
|
+
}
|
|
@@ -25,8 +25,7 @@ export PATH="${NODE_EXECUTABLE:h}:${PATH:-/usr/bin:/bin:/usr/sbin:/sbin}"
|
|
|
25
25
|
"${PROJECT_DIR}/scripts/install-app.sh"
|
|
26
26
|
"${NODE_EXECUTABLE}" "${PROJECT_DIR}/scripts/install-codex-hooks.mjs" "${PROJECT_DIR}/.build/release/cc-pets"
|
|
27
27
|
"${NODE_EXECUTABLE}" "${PROJECT_DIR}/scripts/install-claude-hooks.mjs" \
|
|
28
|
-
"${PROJECT_DIR}/.build/release/cc-pets"
|
|
29
|
-
"${PROJECT_DIR}/bin/claude-statusline-with-pet"
|
|
28
|
+
"${PROJECT_DIR}/.build/release/cc-pets"
|
|
30
29
|
"${NODE_EXECUTABLE}" "${PROJECT_DIR}/scripts/configure-updater.mjs"
|
|
31
30
|
|
|
32
31
|
PET_APP="${PROJECT_DIR}/.build/release/CC Pets.app"
|
|
@@ -15,6 +15,9 @@ const currentClaudeHook = { marker: "CC_PETS_CLAUDE_AGENT_HOOK=1", executable: "
|
|
|
15
15
|
const legacyClaudeHook = { marker: "CLAUDE_PET_AGENT_HOOK=1", executable: "codex-pet" };
|
|
16
16
|
const currentStatusLineMarker = "CC_PETS_CLAUDE_STATUS_LINE=1";
|
|
17
17
|
const legacyStatusLineMarker = "CLAUDE_PET_STATUS_LINE=1";
|
|
18
|
+
const statusLineStartMarker = "# >>> cc-pets-statusline >>>";
|
|
19
|
+
const statusLineEndMarker = "# <<< cc-pets-statusline <<<";
|
|
20
|
+
const createdStatusLineMarker = "# CC Pets created this status line script";
|
|
18
21
|
|
|
19
22
|
function isManagedHookCommand(value, signature) {
|
|
20
23
|
return typeof value === "string" &&
|
|
@@ -28,6 +31,43 @@ function isManagedStatusLine(value, marker) {
|
|
|
28
31
|
/\/bin\/claude-statusline-with-pet' '[A-Za-z0-9+/=]*'$/.test(value);
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
function resolveStatusLineScript(value) {
|
|
35
|
+
if (typeof value !== "string") return null;
|
|
36
|
+
let candidate = value.trim();
|
|
37
|
+
const quoted = candidate.match(/^(['"])(.*)\1$/s);
|
|
38
|
+
if (quoted) candidate = quoted[2];
|
|
39
|
+
if (candidate.startsWith("~/")) candidate = path.join(os.homedir(), candidate.slice(2));
|
|
40
|
+
if (!path.isAbsolute(candidate) || /[\s;&|<>`$()]/.test(candidate)) return null;
|
|
41
|
+
return path.resolve(candidate);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function removeStatusLinePrelude(scriptPath) {
|
|
45
|
+
if (!scriptPath || !fs.existsSync(scriptPath)) return { changed: false, created: false };
|
|
46
|
+
const stats = fs.statSync(scriptPath);
|
|
47
|
+
if (!stats.isFile()) return { changed: false, created: false };
|
|
48
|
+
const original = fs.readFileSync(scriptPath, "utf8");
|
|
49
|
+
const expression = new RegExp(
|
|
50
|
+
`(?:^|\\n)${escapeRegExp(statusLineStartMarker)}\\n[\\s\\S]*?\\n${escapeRegExp(statusLineEndMarker)}(?=\\n|$)`,
|
|
51
|
+
"g"
|
|
52
|
+
);
|
|
53
|
+
const updated = original
|
|
54
|
+
.replace(expression, "")
|
|
55
|
+
.replace(/^\n+/, "")
|
|
56
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
57
|
+
.replace(/\n*$/, "\n");
|
|
58
|
+
if (updated === original) return { changed: false, created: false };
|
|
59
|
+
const created = updated === `#!/bin/zsh\n${createdStatusLineMarker}\n`;
|
|
60
|
+
if (created) {
|
|
61
|
+
fs.unlinkSync(scriptPath);
|
|
62
|
+
} else {
|
|
63
|
+
const temporaryPath = `${scriptPath}.cc-pets.tmp`;
|
|
64
|
+
fs.writeFileSync(temporaryPath, updated, { mode: stats.mode });
|
|
65
|
+
fs.renameSync(temporaryPath, scriptPath);
|
|
66
|
+
fs.chmodSync(scriptPath, stats.mode);
|
|
67
|
+
}
|
|
68
|
+
return { changed: true, created };
|
|
69
|
+
}
|
|
70
|
+
|
|
31
71
|
function writeJSON(filePath, value) {
|
|
32
72
|
const temporaryPath = `${filePath}.cc-pets.tmp`;
|
|
33
73
|
fs.writeFileSync(temporaryPath, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
@@ -96,6 +136,12 @@ const claudeChanged = updateJSON(claudeSettingsPath, (config) => {
|
|
|
96
136
|
else delete config.statusLine;
|
|
97
137
|
changed = true;
|
|
98
138
|
}
|
|
139
|
+
if (!preparingInstall) {
|
|
140
|
+
const scriptPath = resolveStatusLineScript(config.statusLine?.command);
|
|
141
|
+
const statusLineResult = removeStatusLinePrelude(scriptPath);
|
|
142
|
+
if (statusLineResult.created) delete config.statusLine;
|
|
143
|
+
if (statusLineResult.changed) changed = true;
|
|
144
|
+
}
|
|
99
145
|
return changed;
|
|
100
146
|
});
|
|
101
147
|
|