cc-pets 2.2.1 → 2.4.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/CC_BRIDGE.md +24 -2
- package/CC_BRIDGE.zh-CN.md +19 -2
- package/CHANGELOG.md +111 -0
- package/CHANGELOG.zh-CN.md +111 -0
- package/CONTRIBUTING.md +35 -10
- package/CONTRIBUTING.zh-CN.md +28 -10
- package/README.md +212 -32
- package/README.zh-CN.md +167 -36
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +699 -20
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +90 -25
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +71 -23
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +87 -102
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +121 -51
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +17 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +74 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +43 -23
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +832 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +59 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +576 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +507 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +106 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +897 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +2 -1
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +148 -36
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +11 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +646 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +33 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +117 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +44 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +279 -0
- package/Sources/CCPets/UI/CCPetsGlassView.h +57 -0
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +60 -11
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +64 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +1412 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +135 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +865 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +288 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +712 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +97 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +323 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +174 -19
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +598 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +713 -0
- package/THIRD_PARTY_NOTICES.md +8 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -2
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +12 -2
- package/bin/codex-with-pet +15 -5
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +31 -5
- package/scripts/compile-cached.zsh +133 -0
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsGlassView.h +0 -31
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1839
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
package/scripts/pet-store.mjs
DELETED
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// cc-pets 自己的素材仓库。素材可以来自多个"源"(source),但一律落到 ~/.cc-pets/pets,
|
|
4
|
-
// 不碰 ~/.petdex/pets 和 ~/.codex/pets——那两个目录是别的工具在写,混在一起就没法
|
|
5
|
-
// "只看自己装的宠物"。桌宠端(discoverPetOptions)只要发现这个目录有内容,就只认它。
|
|
6
|
-
//
|
|
7
|
-
// 目录名保持素材本名(boba 就是 boba),来源记在宠物目录内的 .source.json 里。这样
|
|
8
|
-
// 桌宠端不需要知道"源"这回事,右键菜单里也不会出现 boba@petdex 这种名字;同名素材
|
|
9
|
-
// 的冲突交给 --as 显式改名解决,那是低频事件,不值得为它把所有目录名都变丑。
|
|
10
|
-
|
|
11
|
-
import fs from "node:fs";
|
|
12
|
-
import os from "node:os";
|
|
13
|
-
import path from "node:path";
|
|
14
|
-
import { t } from "./i18n.mjs";
|
|
15
|
-
|
|
16
|
-
// 一个源的完整定义就是这几项。以后接自建 registry 时在这里加一行即可,
|
|
17
|
-
// 命令、目录结构和桌宠端都不用动。
|
|
18
|
-
const SOURCES = {
|
|
19
|
-
petdex: {
|
|
20
|
-
label: "PetDex",
|
|
21
|
-
manifestUrl: "https://petdex.dev/api/manifest",
|
|
22
|
-
// 清单是远端数据,理论上可以指向任意地址。下载前把 host 卡死,
|
|
23
|
-
// 免得一次清单投毒就变成"从任意地址往本地写文件"。
|
|
24
|
-
assetHosts: ["assets.petdex.dev"],
|
|
25
|
-
referer: "https://petdex.dev/",
|
|
26
|
-
homepage: "https://petdex.dev"
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
// 请求素材源时自报家门:对方运维从日志里能看出流量来自 CC Pets 而不是一串默认 UA,
|
|
30
|
-
// 想联系或限流都有个明确的对象。版本号跟着 package.json 走,不另外维护一份。
|
|
31
|
-
const USER_AGENT = (() => {
|
|
32
|
-
let version = "0.0.0";
|
|
33
|
-
try {
|
|
34
|
-
version = JSON.parse(
|
|
35
|
-
fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")
|
|
36
|
-
).version;
|
|
37
|
-
} catch {
|
|
38
|
-
// 版本读不到不该让下载失败,退回占位版本号即可。
|
|
39
|
-
}
|
|
40
|
-
return `cc-pets/${version} (+https://github.com/Sunnyshinnny776/cc-pets)`;
|
|
41
|
-
})();
|
|
42
|
-
|
|
43
|
-
// 不能 add、只会出现在已装素材里的来源。桌宠的"导入 Codex 素材"开关会写 source: "codex"。
|
|
44
|
-
const LOCAL_SOURCE_LABELS = { codex: t("Codex (imported)") };
|
|
45
|
-
const DEFAULT_SOURCE = process.env.CC_PETS_SOURCE || "petdex";
|
|
46
|
-
const SIDECAR_NAME = ".source.json";
|
|
47
|
-
const MANIFEST_TTL_MS = 6 * 60 * 60 * 1000;
|
|
48
|
-
|
|
49
|
-
const untilde = (value) => (value.startsWith("~/") ? path.join(os.homedir(), value.slice(2)) : value);
|
|
50
|
-
const petsRoot = () => {
|
|
51
|
-
const override = process.env.CC_PETS_PETS_DIR;
|
|
52
|
-
if (override && override.length > 0) return path.resolve(untilde(override));
|
|
53
|
-
return path.join(os.homedir(), ".cc-pets", "pets");
|
|
54
|
-
};
|
|
55
|
-
// 与 CC Bridge、cc-pets clean 一致:CC_PETS_HOME 可覆盖 ~/.cc-pets。
|
|
56
|
-
const cachePath = (sourceName) =>
|
|
57
|
-
path.join(process.env.CC_PETS_HOME || path.join(os.homedir(), ".cc-pets"), "cache", `manifest-${sourceName}.json`);
|
|
58
|
-
// 每个源都可以单独用环境变量改清单地址,主要给测试和自建源的本地联调用。
|
|
59
|
-
const manifestUrlFor = (sourceName) =>
|
|
60
|
-
process.env[`CC_PETS_MANIFEST_URL_${sourceName.toUpperCase()}`] || SOURCES[sourceName].manifestUrl;
|
|
61
|
-
|
|
62
|
-
const dim = (text) => `[2m${text}[0m`;
|
|
63
|
-
const cyan = (text) => `[36m${text}[0m`;
|
|
64
|
-
const green = (text) => `[32m${text}[0m`;
|
|
65
|
-
const red = (text) => `[31m${text}[0m`;
|
|
66
|
-
|
|
67
|
-
const fail = (message) => {
|
|
68
|
-
console.error(red(message));
|
|
69
|
-
process.exit(1);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
// slug 直接用作目录名,而它来自远端清单,必须自己校验,不能指望上游。
|
|
73
|
-
const isSafeSlug = (slug) =>
|
|
74
|
-
typeof slug === "string" && /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(slug) && !slug.includes("..");
|
|
75
|
-
|
|
76
|
-
// 本地名由用户通过 --as 指定,可以用中文等 Unicode 字母。路径分隔符、空白和
|
|
77
|
-
// 控制字符不在允许集内;首字符必须是字母或数字,避免生成隐藏目录。
|
|
78
|
-
const isSafeLocalName = (name) =>
|
|
79
|
-
typeof name === "string" &&
|
|
80
|
-
/^[\p{L}\p{N}][\p{L}\p{N}\p{M}._-]*$/u.test(name) &&
|
|
81
|
-
!name.includes("..");
|
|
82
|
-
|
|
83
|
-
// "petdex:boba" -> { source: "petdex", slug: "boba" };不带前缀就用默认源。
|
|
84
|
-
function parseSpec(spec) {
|
|
85
|
-
const separator = spec.indexOf(":");
|
|
86
|
-
if (separator === -1) return { source: DEFAULT_SOURCE, slug: spec };
|
|
87
|
-
const source = spec.slice(0, separator);
|
|
88
|
-
const slug = spec.slice(separator + 1);
|
|
89
|
-
if (!SOURCES[source]) {
|
|
90
|
-
fail(t('Unknown pet source "{source}". Available sources: {SOURCES}', { source, SOURCES: Object.keys(SOURCES).join(", ") }));
|
|
91
|
-
}
|
|
92
|
-
return { source, slug };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async function fetchManifest(sourceName, { refresh }) {
|
|
96
|
-
const cache = cachePath(sourceName);
|
|
97
|
-
if (!refresh) {
|
|
98
|
-
try {
|
|
99
|
-
const stat = fs.statSync(cache);
|
|
100
|
-
if (Date.now() - stat.mtimeMs < MANIFEST_TTL_MS) {
|
|
101
|
-
const cached = JSON.parse(fs.readFileSync(cache, "utf8"));
|
|
102
|
-
if (Array.isArray(cached?.pets)) return cached.pets;
|
|
103
|
-
}
|
|
104
|
-
} catch {
|
|
105
|
-
// 缓存缺失或损坏都当作没有缓存,直接回源。
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const response = await fetch(manifestUrlFor(sourceName), {
|
|
109
|
-
redirect: "follow",
|
|
110
|
-
headers: { "User-Agent": USER_AGENT }
|
|
111
|
-
});
|
|
112
|
-
if (!response.ok) fail(t("Failed to fetch the {label} pet list: HTTP {status}", { label: SOURCES[sourceName].label, status: response.status }));
|
|
113
|
-
const body = await response.json();
|
|
114
|
-
if (!Array.isArray(body?.pets)) fail(t("The {label} pet list has an unexpected format", { label: SOURCES[sourceName].label }));
|
|
115
|
-
fs.mkdirSync(path.dirname(cache), { recursive: true });
|
|
116
|
-
fs.writeFileSync(cache, JSON.stringify(body));
|
|
117
|
-
return body.pets;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async function download(url, source) {
|
|
121
|
-
let parsed;
|
|
122
|
-
try {
|
|
123
|
-
parsed = new URL(url);
|
|
124
|
-
} catch {
|
|
125
|
-
throw new Error(t("Invalid pet URL: {url}", { url }));
|
|
126
|
-
}
|
|
127
|
-
if (parsed.protocol !== "https:" || !source.assetHosts.includes(parsed.hostname)) {
|
|
128
|
-
throw new Error(t("Untrusted pet source: {url}", { url: parsed.hostname || url }));
|
|
129
|
-
}
|
|
130
|
-
const response = await fetch(parsed, {
|
|
131
|
-
headers: { Referer: source.referer, "User-Agent": USER_AGENT }
|
|
132
|
-
});
|
|
133
|
-
if (!response.ok) throw new Error(t("Download failed {pathname} -> HTTP {status}", { pathname: parsed.pathname, status: response.status }));
|
|
134
|
-
return Buffer.from(await response.arrayBuffer());
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function readSidecar(directory) {
|
|
138
|
-
try {
|
|
139
|
-
const raw = JSON.parse(fs.readFileSync(path.join(directory, SIDECAR_NAME), "utf8"));
|
|
140
|
-
return typeof raw?.source === "string" ? raw : null;
|
|
141
|
-
} catch {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 已经存在的目录不能闷声覆盖:可能是另一个源的同名素材,也可能是用户自己手放的图。
|
|
147
|
-
function assertInstallable(directory, sourceName, localName) {
|
|
148
|
-
if (!fs.existsSync(directory)) return;
|
|
149
|
-
const sidecar = readSidecar(directory);
|
|
150
|
-
if (sidecar?.source === sourceName) return;
|
|
151
|
-
const owner = sidecar ? t("came from {source}", { source: sidecar.source }) : t("wasn't installed by cc-pets pet (no source record)");
|
|
152
|
-
throw new Error(
|
|
153
|
-
t("{localName} already exists and {owner}. Install it under another name: ", { localName, owner }) +
|
|
154
|
-
`cc-pets pet add ${sourceName}:${localName} --as ${localName}-${sourceName}`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async function installPet(pet, sourceName, localName) {
|
|
159
|
-
const source = SOURCES[sourceName];
|
|
160
|
-
if (!isSafeSlug(pet.slug)) throw new Error(t("Invalid pet name: {slug}", { slug: pet.slug }));
|
|
161
|
-
const directory = path.join(petsRoot(), localName);
|
|
162
|
-
assertInstallable(directory, sourceName, localName);
|
|
163
|
-
|
|
164
|
-
const [meta, sprite] = await Promise.all([
|
|
165
|
-
download(pet.petJsonUrl, source),
|
|
166
|
-
download(pet.spritesheetUrl, source)
|
|
167
|
-
]);
|
|
168
|
-
const extension = new URL(pet.spritesheetUrl).pathname.endsWith(".png") ? "png" : "webp";
|
|
169
|
-
fs.mkdirSync(directory, { recursive: true });
|
|
170
|
-
// 同一只宠物换过格式时,旧的 spritesheet 会留下来抢加载顺序,先清掉。
|
|
171
|
-
for (const stale of ["spritesheet.webp", "spritesheet.png"]) {
|
|
172
|
-
if (stale !== `spritesheet.${extension}`) fs.rmSync(path.join(directory, stale), { force: true });
|
|
173
|
-
}
|
|
174
|
-
// pet.json 原样落盘,不掺自己的字段——保持上游原始内容,以后比对和更新才干净。
|
|
175
|
-
fs.writeFileSync(path.join(directory, "pet.json"), meta);
|
|
176
|
-
fs.writeFileSync(path.join(directory, `spritesheet.${extension}`), sprite);
|
|
177
|
-
fs.writeFileSync(
|
|
178
|
-
path.join(directory, SIDECAR_NAME),
|
|
179
|
-
`${JSON.stringify({ source: sourceName, slug: pet.slug, installedAt: new Date().toISOString() }, null, 2)}\n`
|
|
180
|
-
);
|
|
181
|
-
return directory;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function installedPets() {
|
|
185
|
-
const root = petsRoot();
|
|
186
|
-
let entries;
|
|
187
|
-
try {
|
|
188
|
-
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
189
|
-
} catch {
|
|
190
|
-
return [];
|
|
191
|
-
}
|
|
192
|
-
return entries
|
|
193
|
-
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
194
|
-
.map((entry) => {
|
|
195
|
-
const sidecar = readSidecar(path.join(root, entry.name));
|
|
196
|
-
return { name: entry.name, source: sidecar?.source ?? null, slug: sidecar?.slug ?? null };
|
|
197
|
-
})
|
|
198
|
-
.sort((a, b) => a.name.localeCompare(b.name));
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function commandAdd(args) {
|
|
202
|
-
const refresh = args.includes("--refresh");
|
|
203
|
-
const asIndex = args.indexOf("--as");
|
|
204
|
-
const alias = asIndex === -1 ? null : args[asIndex + 1];
|
|
205
|
-
if (asIndex !== -1 && (!alias || alias.startsWith("--"))) fail(t("--as needs a name after it."));
|
|
206
|
-
if (alias && !isSafeLocalName(alias)) fail(t("Invalid --as name: {alias}", { alias }));
|
|
207
|
-
|
|
208
|
-
// asIndex 为 -1 时 asIndex + 1 是 0,会把第一个位置参数当成 --as 的取值吃掉。
|
|
209
|
-
const aliasValueIndex = asIndex === -1 ? -1 : asIndex + 1;
|
|
210
|
-
const positional = args.filter(
|
|
211
|
-
(value, index) => !value.startsWith("--") && index !== aliasValueIndex);
|
|
212
|
-
const specs = [...new Set(positional)];
|
|
213
|
-
if (specs.length === 0) fail(t("Usage: {cyan}", { cyan: cyan(t("cc-pets pet add [source:]<name> [name...] [--as <local-name>]")) }));
|
|
214
|
-
if (alias && specs.length > 1) fail(t("--as can only rename one pet at a time."));
|
|
215
|
-
|
|
216
|
-
// 按源分组,同一个源只拉一次清单。
|
|
217
|
-
const wanted = specs.map(parseSpec);
|
|
218
|
-
const bySource = new Map();
|
|
219
|
-
for (const item of wanted) {
|
|
220
|
-
if (!bySource.has(item.source)) bySource.set(item.source, []);
|
|
221
|
-
bySource.get(item.source).push(item.slug);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
let failed = 0;
|
|
225
|
-
let installed = 0;
|
|
226
|
-
for (const [sourceName, slugs] of bySource) {
|
|
227
|
-
const pets = await fetchManifest(sourceName, { refresh });
|
|
228
|
-
const index = new Map(pets.map((pet) => [pet.slug, pet]));
|
|
229
|
-
for (const slug of slugs) {
|
|
230
|
-
const pet = index.get(slug);
|
|
231
|
-
if (!pet) {
|
|
232
|
-
failed += 1;
|
|
233
|
-
console.error(t("{red} {sourceName}:{slug}: not in the pet list", { red: red("×"), sourceName, slug }));
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
const localName = alias ?? slug;
|
|
237
|
-
try {
|
|
238
|
-
const directory = await installPet(pet, sourceName, localName);
|
|
239
|
-
installed += 1;
|
|
240
|
-
const renamed = localName === slug ? "" : dim(` (${sourceName}:${slug})`);
|
|
241
|
-
console.log(`${green("✓")} ${localName}${renamed} ${dim(`→ ${directory}`)}`);
|
|
242
|
-
} catch (error) {
|
|
243
|
-
failed += 1;
|
|
244
|
-
console.error(`${red("×")} ${sourceName}:${slug}: ${error.message}`);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (installed > 0) {
|
|
250
|
-
console.log(dim(t("Pets directory: {petsRoot}", { petsRoot: petsRoot() })));
|
|
251
|
-
console.log(dim(t("Click “Rescan” in the pet menu or restart the pet to see new pets.")));
|
|
252
|
-
}
|
|
253
|
-
if (failed > 0) process.exit(1);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async function commandSearch(args) {
|
|
257
|
-
const refresh = args.includes("--refresh");
|
|
258
|
-
const positional = args.filter((value) => !value.startsWith("--"));
|
|
259
|
-
// 关键词本身也可以带源前缀:`cc-pets pet search petdex:otter`。
|
|
260
|
-
const scoped = positional.length > 0 && positional[0].includes(":")
|
|
261
|
-
? parseSpec(positional[0])
|
|
262
|
-
: { source: DEFAULT_SOURCE, slug: positional.join(" ") };
|
|
263
|
-
const sourceName = scoped.source;
|
|
264
|
-
const keyword = [scoped.slug, ...positional.slice(1)].join(" ").trim().toLowerCase();
|
|
265
|
-
|
|
266
|
-
const pets = await fetchManifest(sourceName, { refresh });
|
|
267
|
-
const matched = keyword.length === 0
|
|
268
|
-
? pets
|
|
269
|
-
: pets.filter((pet) =>
|
|
270
|
-
`${pet.slug} ${pet.displayName ?? ""} ${pet.submittedBy ?? ""}`.toLowerCase().includes(keyword));
|
|
271
|
-
if (matched.length === 0) fail(t('No pets on {label} match "{keyword}".', { label: SOURCES[sourceName].label, keyword }));
|
|
272
|
-
const shown = matched.slice(0, 60);
|
|
273
|
-
for (const pet of shown) {
|
|
274
|
-
const author = pet.submittedBy ? dim(` by ${pet.submittedBy}`) : "";
|
|
275
|
-
console.log(` ${cyan(pet.slug)} ${pet.displayName ?? ""}${author}`);
|
|
276
|
-
}
|
|
277
|
-
if (matched.length > shown.length) {
|
|
278
|
-
console.log(dim(t(" … {length} results, showing the first {shown}.", { length: matched.length, shown: shown.length })));
|
|
279
|
-
}
|
|
280
|
-
console.log(dim(t("Install one: cc-pets pet add {sourceName}:{slug}", { sourceName, slug: shown[0].slug })));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function commandList() {
|
|
284
|
-
const pets = installedPets();
|
|
285
|
-
if (pets.length === 0) {
|
|
286
|
-
console.log(dim(t("No pets in {petsRoot} yet.", { petsRoot: petsRoot() })));
|
|
287
|
-
console.log(dim(t("Try: cc-pets pet add boba")));
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
const groups = new Map();
|
|
291
|
-
for (const pet of pets) {
|
|
292
|
-
const key = pet.source ?? "__unknown__";
|
|
293
|
-
if (!groups.has(key)) groups.set(key, []);
|
|
294
|
-
groups.get(key).push(pet);
|
|
295
|
-
}
|
|
296
|
-
// 有来源记录的源按名字排在前面,手放的素材垫底。
|
|
297
|
-
const keys = [...groups.keys()].sort((a, b) => {
|
|
298
|
-
if (a === "__unknown__") return 1;
|
|
299
|
-
if (b === "__unknown__") return -1;
|
|
300
|
-
return a.localeCompare(b);
|
|
301
|
-
});
|
|
302
|
-
for (const key of keys) {
|
|
303
|
-
const label = key === "__unknown__"
|
|
304
|
-
? t("Local pets (no source record)")
|
|
305
|
-
: SOURCES[key]?.label ?? LOCAL_SOURCE_LABELS[key] ?? key;
|
|
306
|
-
console.log(` ${label}`);
|
|
307
|
-
for (const pet of groups.get(key)) {
|
|
308
|
-
// 目录名和上游 slug 不一致时(--as 改过名)把原名标出来。
|
|
309
|
-
const origin = pet.slug && pet.slug !== pet.name ? dim(` (${key}:${pet.slug})`) : "";
|
|
310
|
-
console.log(` ${cyan(pet.name)}${origin}`);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
console.log(dim(t("{length} in total, in {petsRoot}", { length: pets.length, petsRoot: petsRoot() })));
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function commandRemove(args) {
|
|
317
|
-
const names = args.filter((value) => !value.startsWith("--"));
|
|
318
|
-
if (names.length === 0) fail(t("Usage: {cyan}", { cyan: cyan(t("cc-pets pet remove <name> [name...]")) }));
|
|
319
|
-
const root = petsRoot();
|
|
320
|
-
let removed = 0;
|
|
321
|
-
for (const name of names) {
|
|
322
|
-
if (!isSafeLocalName(name)) {
|
|
323
|
-
console.error(t("{red} {name}: invalid name", { red: red("×"), name }));
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
const directory = path.join(root, name);
|
|
327
|
-
if (!fs.existsSync(directory)) {
|
|
328
|
-
console.error(t("{red} {name}: not installed", { red: red("×"), name }));
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
const sidecar = readSidecar(directory);
|
|
332
|
-
fs.rmSync(directory, { recursive: true, force: true });
|
|
333
|
-
removed += 1;
|
|
334
|
-
console.log(t("{green} Removed {name}{source}", { green: green("✓"), name, source: sidecar ? dim(t(" (from {source})", { source: sidecar.source })) : "" }));
|
|
335
|
-
}
|
|
336
|
-
if (removed < names.length) process.exit(1);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function commandSource() {
|
|
340
|
-
for (const [name, source] of Object.entries(SOURCES)) {
|
|
341
|
-
const marker = name === DEFAULT_SOURCE ? green(t(" (default)")) : "";
|
|
342
|
-
console.log(` ${cyan(name)}${marker} ${source.label} ${dim(source.homepage)}`);
|
|
343
|
-
}
|
|
344
|
-
console.log(dim(t("Change the default source with CC_PETS_SOURCE, or prefix a name with `source:`.")));
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
function commandDir() {
|
|
348
|
-
const root = petsRoot();
|
|
349
|
-
fs.mkdirSync(root, { recursive: true });
|
|
350
|
-
console.log(root);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function usage() {
|
|
354
|
-
console.log([
|
|
355
|
-
"",
|
|
356
|
-
t(" {cyan} — install pets into cc-pets' own directory", { cyan: cyan("cc-pets pet") }),
|
|
357
|
-
"",
|
|
358
|
-
t(" {cyan} [source:]<name>... download pets to {petsRoot}", { cyan: cyan("add"), petsRoot: dim(petsRoot()) }),
|
|
359
|
-
t(" {cyan} [source:][keyword] search pets (no keyword lists all)", { cyan: cyan("search") }),
|
|
360
|
-
t(" {cyan} list installed pets, grouped by source", { cyan: cyan("list") }),
|
|
361
|
-
t(" {cyan} <name>... remove installed pets", { cyan: cyan("remove") }),
|
|
362
|
-
t(" {cyan} list available sources", { cyan: cyan("source") }),
|
|
363
|
-
t(" {cyan} print the pets directory", { cyan: cyan("dir") }),
|
|
364
|
-
"",
|
|
365
|
-
` ${dim(t("Names can have a source prefix (petdex:boba); otherwise the default source is used: ") + DEFAULT_SOURCE + t("."))}`,
|
|
366
|
-
` ${dim(t("--as <local-name> installs under another directory name (any language) to avoid name clashes."))}`,
|
|
367
|
-
` ${dim(t("--refresh forces a refresh of the pet list cache (cached for 6 hours by default)."))}`,
|
|
368
|
-
` ${dim(t("CC_PETS_PETS_DIR changes the pets directory; CC_PETS_SOURCE changes the default source."))}`,
|
|
369
|
-
"",
|
|
370
|
-
` ${dim("$")} cc-pets pet search otter`,
|
|
371
|
-
` ${dim("$")} cc-pets pet add boba`,
|
|
372
|
-
` ${dim("$")} cc-pets pet add petdex:boba --as boba-petdex`,
|
|
373
|
-
t(" {dim} cc-pets pet add boba --as bubble-tea", { dim: dim("$") }),
|
|
374
|
-
""
|
|
375
|
-
].join("\n"));
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const [command, ...args] = process.argv.slice(2);
|
|
379
|
-
if (!SOURCES[DEFAULT_SOURCE]) {
|
|
380
|
-
fail(t('CC_PETS_SOURCE points to an unknown source "{DEFAULT_SOURCE}". Available sources: {SOURCES}', { DEFAULT_SOURCE, SOURCES: Object.keys(SOURCES).join(", ") }));
|
|
381
|
-
}
|
|
382
|
-
switch (command) {
|
|
383
|
-
case "add":
|
|
384
|
-
case "install":
|
|
385
|
-
await commandAdd(args);
|
|
386
|
-
break;
|
|
387
|
-
case "search":
|
|
388
|
-
await commandSearch(args);
|
|
389
|
-
break;
|
|
390
|
-
case "list":
|
|
391
|
-
case "ls":
|
|
392
|
-
commandList();
|
|
393
|
-
break;
|
|
394
|
-
case "remove":
|
|
395
|
-
case "rm":
|
|
396
|
-
commandRemove(args);
|
|
397
|
-
break;
|
|
398
|
-
case "source":
|
|
399
|
-
case "sources":
|
|
400
|
-
commandSource();
|
|
401
|
-
break;
|
|
402
|
-
case "dir":
|
|
403
|
-
commandDir();
|
|
404
|
-
break;
|
|
405
|
-
case undefined:
|
|
406
|
-
case "help":
|
|
407
|
-
case "--help":
|
|
408
|
-
case "-h":
|
|
409
|
-
usage();
|
|
410
|
-
break;
|
|
411
|
-
default:
|
|
412
|
-
console.error(red(t("Unknown subcommand: {command}", { command })));
|
|
413
|
-
usage();
|
|
414
|
-
process.exit(2);
|
|
415
|
-
}
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import os from "node:os";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
import {
|
|
7
|
-
CLAUDE_HOOK, CODEX_HOOK, CREATED_STATUS_LINE_MARKER, LEGACY_CLAUDE_HOOK, LEGACY_CODEX_HOOK,
|
|
8
|
-
LEGACY_STATUS_LINE_MARKER, SHIM_END_MARKER, SHIM_START_MARKER, STATUS_LINE_END_MARKER,
|
|
9
|
-
STATUS_LINE_MARKER, STATUS_LINE_START_MARKER, isManagedHookCommand, isManagedStatusLine,
|
|
10
|
-
resolveStatusLineScript
|
|
11
|
-
} from "./integration-markers.mjs";
|
|
12
|
-
import { t } from "./i18n.mjs";
|
|
13
|
-
|
|
14
|
-
const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
15
|
-
const claudeHome = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude");
|
|
16
|
-
const preparingInstall = process.argv[2] === "--prepare-install";
|
|
17
|
-
const shellRC = process.argv[preparingInstall ? 3 : 2];
|
|
18
|
-
|
|
19
|
-
const currentCodexHook = CODEX_HOOK;
|
|
20
|
-
const legacyCodexHook = LEGACY_CODEX_HOOK;
|
|
21
|
-
const currentClaudeHook = CLAUDE_HOOK;
|
|
22
|
-
const legacyClaudeHook = LEGACY_CLAUDE_HOOK;
|
|
23
|
-
const currentStatusLineMarker = STATUS_LINE_MARKER;
|
|
24
|
-
const legacyStatusLineMarker = LEGACY_STATUS_LINE_MARKER;
|
|
25
|
-
const statusLineStartMarker = STATUS_LINE_START_MARKER;
|
|
26
|
-
const statusLineEndMarker = STATUS_LINE_END_MARKER;
|
|
27
|
-
const createdStatusLineMarker = CREATED_STATUS_LINE_MARKER;
|
|
28
|
-
|
|
29
|
-
function removeStatusLinePrelude(scriptPath) {
|
|
30
|
-
if (!scriptPath || !fs.existsSync(scriptPath)) return { changed: false, created: false };
|
|
31
|
-
const stats = fs.statSync(scriptPath);
|
|
32
|
-
if (!stats.isFile()) return { changed: false, created: false };
|
|
33
|
-
const original = fs.readFileSync(scriptPath, "utf8");
|
|
34
|
-
const expression = new RegExp(
|
|
35
|
-
`(?:^|\\n)${escapeRegExp(statusLineStartMarker)}\\n[\\s\\S]*?\\n${escapeRegExp(statusLineEndMarker)}(?=\\n|$)`,
|
|
36
|
-
"g"
|
|
37
|
-
);
|
|
38
|
-
const updated = original
|
|
39
|
-
.replace(expression, "")
|
|
40
|
-
.replace(/^\n+/, "")
|
|
41
|
-
.replace(/\n{3,}/g, "\n\n")
|
|
42
|
-
.replace(/\n*$/, "\n");
|
|
43
|
-
if (updated === original) return { changed: false, created: false };
|
|
44
|
-
const created = updated === `#!/bin/zsh\n${createdStatusLineMarker}\n`;
|
|
45
|
-
if (created) {
|
|
46
|
-
fs.unlinkSync(scriptPath);
|
|
47
|
-
} else {
|
|
48
|
-
const temporaryPath = `${scriptPath}.cc-pets.tmp`;
|
|
49
|
-
fs.writeFileSync(temporaryPath, updated, { mode: stats.mode });
|
|
50
|
-
fs.renameSync(temporaryPath, scriptPath);
|
|
51
|
-
fs.chmodSync(scriptPath, stats.mode);
|
|
52
|
-
}
|
|
53
|
-
return { changed: true, created };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function writeJSON(filePath, value) {
|
|
57
|
-
const temporaryPath = `${filePath}.cc-pets.tmp`;
|
|
58
|
-
fs.writeFileSync(temporaryPath, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
59
|
-
fs.renameSync(temporaryPath, filePath);
|
|
60
|
-
fs.chmodSync(filePath, 0o600);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function removeHookHandlers(config, markers) {
|
|
64
|
-
if (!config?.hooks || typeof config.hooks !== "object" || Array.isArray(config.hooks)) return false;
|
|
65
|
-
let changed = false;
|
|
66
|
-
for (const [event, groups] of Object.entries(config.hooks)) {
|
|
67
|
-
if (!Array.isArray(groups)) continue;
|
|
68
|
-
const filteredGroups = groups.flatMap((group) => {
|
|
69
|
-
if (!group || !Array.isArray(group.hooks)) return [group];
|
|
70
|
-
const handlers = group.hooks.filter((handler) =>
|
|
71
|
-
!markers.some((signature) => isManagedHookCommand(handler?.command, signature))
|
|
72
|
-
);
|
|
73
|
-
if (handlers.length !== group.hooks.length) changed = true;
|
|
74
|
-
return handlers.length > 0 ? [{ ...group, hooks: handlers }] : [];
|
|
75
|
-
});
|
|
76
|
-
if (filteredGroups.length > 0) config.hooks[event] = filteredGroups;
|
|
77
|
-
else delete config.hooks[event];
|
|
78
|
-
}
|
|
79
|
-
if (Object.keys(config.hooks).length === 0) delete config.hooks;
|
|
80
|
-
return changed;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function updateJSON(filePath, updater) {
|
|
84
|
-
if (!fs.existsSync(filePath)) return false;
|
|
85
|
-
let config;
|
|
86
|
-
try {
|
|
87
|
-
config = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
88
|
-
} catch (error) {
|
|
89
|
-
console.error(t("Can't parse {filePath}, left unchanged: {message}", { filePath, message: error.message }));
|
|
90
|
-
process.exitCode = 1;
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
if (!config || typeof config !== "object" || Array.isArray(config)) return false;
|
|
94
|
-
if (!updater(config)) return false;
|
|
95
|
-
writeJSON(filePath, config);
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const codexHooksPath = path.join(codexHome, "hooks.json");
|
|
100
|
-
const codexSignatures = preparingInstall
|
|
101
|
-
? [legacyCodexHook]
|
|
102
|
-
: [currentCodexHook, legacyCodexHook];
|
|
103
|
-
const codexChanged = updateJSON(codexHooksPath, (config) =>
|
|
104
|
-
removeHookHandlers(config, codexSignatures)
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
const claudeSettingsPath = path.join(claudeHome, "settings.json");
|
|
108
|
-
const claudeSignatures = preparingInstall
|
|
109
|
-
? [legacyClaudeHook]
|
|
110
|
-
: [currentClaudeHook, legacyClaudeHook];
|
|
111
|
-
const statusLineMarkers = preparingInstall
|
|
112
|
-
? [legacyStatusLineMarker]
|
|
113
|
-
: [currentStatusLineMarker, legacyStatusLineMarker];
|
|
114
|
-
const claudeChanged = updateJSON(claudeSettingsPath, (config) => {
|
|
115
|
-
let changed = removeHookHandlers(config, claudeSignatures);
|
|
116
|
-
const command = typeof config.statusLine?.command === "string" ? config.statusLine.command : "";
|
|
117
|
-
if (statusLineMarkers.some((marker) => isManagedStatusLine(command, marker))) {
|
|
118
|
-
const match = command.match(/'([A-Za-z0-9+/=]*)'\s*$/);
|
|
119
|
-
const originalCommand = match ? Buffer.from(match[1], "base64").toString("utf8") : "";
|
|
120
|
-
if (originalCommand) config.statusLine = { ...config.statusLine, command: originalCommand };
|
|
121
|
-
else delete config.statusLine;
|
|
122
|
-
changed = true;
|
|
123
|
-
}
|
|
124
|
-
if (!preparingInstall) {
|
|
125
|
-
const scriptPath = resolveStatusLineScript(config.statusLine?.command);
|
|
126
|
-
const statusLineResult = removeStatusLinePrelude(scriptPath);
|
|
127
|
-
if (statusLineResult.created) delete config.statusLine;
|
|
128
|
-
if (statusLineResult.changed) changed = true;
|
|
129
|
-
}
|
|
130
|
-
return changed;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
function escapeRegExp(value) {
|
|
134
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function removeMarkedBlock(text, startMarker, endMarker) {
|
|
138
|
-
const expression = new RegExp(
|
|
139
|
-
`(^|\\n)${escapeRegExp(startMarker)}\\n[\\s\\S]*?${escapeRegExp(endMarker)}(?=\\n|$)`,
|
|
140
|
-
"g"
|
|
141
|
-
);
|
|
142
|
-
return text.replace(expression, "");
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
let shellChanged = false;
|
|
146
|
-
if (shellRC && fs.existsSync(shellRC)) {
|
|
147
|
-
const original = fs.readFileSync(shellRC, "utf8");
|
|
148
|
-
// cc-pets-codex / cc-pets-claude 是 alias 时代的段落。alias 区分大小写,挡不住
|
|
149
|
-
// Claude / CODEX 这类写法,已被 cc-pets-shims 的 PATH shim 取代;安装前的
|
|
150
|
-
// --prepare-install 会走到这里,顺带把老段落迁移掉。
|
|
151
|
-
let updated = removeMarkedBlock(original, SHIM_START_MARKER, SHIM_END_MARKER);
|
|
152
|
-
updated = removeMarkedBlock(updated, "# >>> cc-pets-codex >>>", "# <<< cc-pets-codex <<<");
|
|
153
|
-
updated = removeMarkedBlock(updated, "# >>> cc-pets-claude >>>", "# <<< cc-pets-claude <<<");
|
|
154
|
-
updated = removeMarkedBlock(updated, "# >>> codex-pet >>>", "# <<< codex-pet <<<");
|
|
155
|
-
updated = removeMarkedBlock(updated, "# >>> claude-pet >>>", "# <<< claude-pet <<<");
|
|
156
|
-
updated = updated.replace(/\n{3,}/g, "\n\n");
|
|
157
|
-
if (updated !== original) {
|
|
158
|
-
const mode = fs.statSync(shellRC).mode;
|
|
159
|
-
const temporaryPath = `${shellRC}.cc-pets.tmp`;
|
|
160
|
-
fs.writeFileSync(temporaryPath, updated, { mode });
|
|
161
|
-
fs.renameSync(temporaryPath, shellRC);
|
|
162
|
-
shellChanged = true;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// shim 只在真正卸载时删:--prepare-install 之后紧接着就是重建,删了反而在
|
|
167
|
-
// 重建失败时留下一个没有 codex / claude 的 PATH 目录。
|
|
168
|
-
let shimChanged = false;
|
|
169
|
-
const shimDirectory = process.env.CC_PETS_SHIM_DIR || path.join(os.homedir(), ".cc-pets", "shims");
|
|
170
|
-
if (!preparingInstall && fs.existsSync(shimDirectory)) {
|
|
171
|
-
for (const name of ["codex", "claude"]) {
|
|
172
|
-
const shimPath = path.join(shimDirectory, name);
|
|
173
|
-
// lstat:shim 是软链,目标包被 npm uninstall 删掉后 existsSync 会是 false。
|
|
174
|
-
let stats = null;
|
|
175
|
-
try {
|
|
176
|
-
stats = fs.lstatSync(shimPath);
|
|
177
|
-
} catch {
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
if (!stats.isSymbolicLink()) continue;
|
|
181
|
-
if (!fs.readlinkSync(shimPath).endsWith(`/bin/${name}-with-pet`)) continue;
|
|
182
|
-
fs.rmSync(shimPath, { force: true });
|
|
183
|
-
shimChanged = true;
|
|
184
|
-
}
|
|
185
|
-
if (fs.readdirSync(shimDirectory).length === 0) fs.rmdirSync(shimDirectory);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// CC Bridge 只在真正卸载时移除;重装时由 install-shell-integration.sh 的 bridge refresh 接管。
|
|
189
|
-
// MCP 注册只在确实开启过时才去调用 claude / codex 注销,避免卸载时无谓地拉起它们的 CLI。
|
|
190
|
-
if (!preparingInstall) {
|
|
191
|
-
const bridge = await import("./bridge/install.mjs");
|
|
192
|
-
const bridgeStore = await import("./bridge/store.mjs");
|
|
193
|
-
bridgeStore.migrateLegacyFlag();
|
|
194
|
-
for (const line of bridge.uninstall({ unregisterMcp: bridgeStore.isBridgeEnabled() })) console.log(line);
|
|
195
|
-
bridgeStore.setBridgeEnabled(false);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const action = preparingInstall ? t("Migrated") : t("Removed");
|
|
199
|
-
if (codexChanged) console.log(t("{action} the CC Pets hooks in {codexHooksPath}.", { action, codexHooksPath }));
|
|
200
|
-
if (claudeChanged) console.log(t("{action} the CC Pets hooks in {claudeSettingsPath} and restored the original status line.", { action, claudeSettingsPath }));
|
|
201
|
-
if (shellChanged) console.log(t("{action} the CC Pets shell integration in {shellRC}.", { action, shellRC }));
|
|
202
|
-
if (shimChanged) console.log(t("{action} the codex / claude shims in {shimDirectory}.", { action, shimDirectory }));
|
|
203
|
-
if (!codexChanged && !claudeChanged && !shellChanged && !shimChanged && !preparingInstall) {
|
|
204
|
-
console.log(t("No CC Pets integrations to remove. "));
|
|
205
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|