cc-pets 2.2.1 → 2.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/CC_BRIDGE.md +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +60 -0
- package/CHANGELOG.zh-CN.md +60 -0
- package/CONTRIBUTING.md +13 -10
- package/CONTRIBUTING.zh-CN.md +11 -10
- package/README.md +113 -24
- package/README.zh-CN.md +89 -28
- package/Resources/zh-Hans.lproj/Localizable.strings +568 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +82 -23
- 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} +70 -22
- 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} +86 -98
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +8 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +15 -0
- 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} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -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/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- 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 +632 -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 +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -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+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -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} +163 -6
- 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 +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- 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/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/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +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
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
#import "CCPetsL10n.h"
|
|
2
|
+
#import "CCPetsIntegrations.h"
|
|
3
|
+
#import "CCPetsJSON.h"
|
|
4
|
+
#import "CCPetsProfiles.h"
|
|
5
|
+
#import "CCPetsCLISupport.h"
|
|
6
|
+
#import <sys/stat.h>
|
|
7
|
+
#import <fcntl.h>
|
|
8
|
+
#import <unistd.h>
|
|
9
|
+
#import <errno.h>
|
|
10
|
+
|
|
11
|
+
NSString *const CCPetsClaudeHookMarker = @"CC_PETS_CLAUDE_AGENT_HOOK=1";
|
|
12
|
+
NSString *const CCPetsLegacyClaudeHookMarker = @"CLAUDE_PET_AGENT_HOOK=1";
|
|
13
|
+
NSString *const CCPetsCodexHookMarker = @"CC_PETS_CODEX_AGENT_HOOK=1";
|
|
14
|
+
NSString *const CCPetsLegacyCodexHookMarker = @"CODEX_PET_AGENT_HOOK=1";
|
|
15
|
+
NSString *const CCPetsStatusLineMarker = @"CC_PETS_CLAUDE_STATUS_LINE=1";
|
|
16
|
+
NSString *const CCPetsLegacyStatusLineMarker = @"CLAUDE_PET_STATUS_LINE=1";
|
|
17
|
+
NSString *const CCPetsStatusLineStartMarker = @"# >>> cc-pets-statusline >>>";
|
|
18
|
+
NSString *const CCPetsStatusLineEndMarker = @"# <<< cc-pets-statusline <<<";
|
|
19
|
+
NSString *const CCPetsCreatedStatusLineMarker = @"# CC Pets created this status line script";
|
|
20
|
+
|
|
21
|
+
// 安装用:读不到文件算空配置,读到了却解析失败要报错退出,直接覆盖会把用户的配置整个冲掉。
|
|
22
|
+
static CCPetsJSONObject *LoadConfig(NSString *path, NSString **errorMessage) {
|
|
23
|
+
NSString *message = nil;
|
|
24
|
+
id config = CCPetsReadConfig(path, &message);
|
|
25
|
+
if (!config && message) {
|
|
26
|
+
if (errorMessage) *errorMessage = message;
|
|
27
|
+
return nil;
|
|
28
|
+
}
|
|
29
|
+
if (![config isKindOfClass:CCPetsJSONObject.class]) config = [CCPetsJSONObject new];
|
|
30
|
+
if (![config[@"hooks"] isKindOfClass:CCPetsJSONObject.class]) config[@"hooks"] = [CCPetsJSONObject new];
|
|
31
|
+
return config;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 从源码目录安装时指向 .build/release,从 App 里修复时指向包内的程序;两种都要认,
|
|
35
|
+
// 否则另一种写进去的条目永远摘不掉,每装一次就多一份。
|
|
36
|
+
BOOL CCPetsIsManagedHookCommand(id value, NSString *marker, NSString *executable) {
|
|
37
|
+
if (![value isKindOfClass:NSString.class] || ![value hasPrefix:[marker stringByAppendingString:@" "]]) return NO;
|
|
38
|
+
for (NSString *directory in @[@"/.build/release/", @".app/Contents/MacOS/"]) {
|
|
39
|
+
if ([value hasSuffix:[NSString stringWithFormat:@"%@%@' --hook", directory, executable]]) return YES;
|
|
40
|
+
}
|
|
41
|
+
return NO;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 摘掉 signatures 认领的 handler,别的工具的原样保留;一个 group 里只剩我们的条目时整个 group 删掉。
|
|
45
|
+
// pruneEmpty 用于卸载:事件下一个 group 都不剩就删掉这个事件,hooks 整个空了也删掉。
|
|
46
|
+
// 返回是否真的摘掉了东西。
|
|
47
|
+
static BOOL RemoveManagedHooks(CCPetsJSONObject *config, NSArray<NSArray<NSString *> *> *signatures, BOOL pruneEmpty) {
|
|
48
|
+
CCPetsJSONObject *hooks = config[@"hooks"];
|
|
49
|
+
if (![hooks isKindOfClass:CCPetsJSONObject.class]) return NO;
|
|
50
|
+
BOOL changed = NO;
|
|
51
|
+
for (NSString *event in hooks.keys) {
|
|
52
|
+
NSArray *groups = hooks[event];
|
|
53
|
+
if (![groups isKindOfClass:NSArray.class]) continue;
|
|
54
|
+
NSMutableArray *kept = [NSMutableArray array];
|
|
55
|
+
for (id group in groups) {
|
|
56
|
+
NSArray *handlers = [group isKindOfClass:CCPetsJSONObject.class] ? ((CCPetsJSONObject *)group)[@"hooks"] : nil;
|
|
57
|
+
if (![handlers isKindOfClass:NSArray.class]) {
|
|
58
|
+
[kept addObject:group];
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
NSMutableArray *remaining = [NSMutableArray array];
|
|
62
|
+
for (id handler in handlers) {
|
|
63
|
+
id handlerCommand = [handler isKindOfClass:CCPetsJSONObject.class] ? ((CCPetsJSONObject *)handler)[@"command"] : nil;
|
|
64
|
+
BOOL managed = NO;
|
|
65
|
+
for (NSArray<NSString *> *signature in signatures) {
|
|
66
|
+
if (CCPetsIsManagedHookCommand(handlerCommand, signature[0], signature[1])) managed = YES;
|
|
67
|
+
}
|
|
68
|
+
if (!managed) [remaining addObject:handler];
|
|
69
|
+
}
|
|
70
|
+
if (remaining.count != handlers.count) changed = YES;
|
|
71
|
+
if (remaining.count == 0) continue;
|
|
72
|
+
CCPetsJSONObject *updated = [group copy];
|
|
73
|
+
updated[@"hooks"] = remaining;
|
|
74
|
+
[kept addObject:updated];
|
|
75
|
+
}
|
|
76
|
+
if (pruneEmpty && kept.count == 0) [hooks removeObjectForKey:event];
|
|
77
|
+
else hooks[event] = kept;
|
|
78
|
+
}
|
|
79
|
+
if (pruneEmpty && hooks.keys.count == 0) [config removeObjectForKey:@"hooks"];
|
|
80
|
+
return changed;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 先摘掉旧的自家条目(含改名前 codex-pet 的),再给每个事件追加一条,重复安装保持幂等。
|
|
84
|
+
static void ReplaceManagedHooks(CCPetsJSONObject *config, NSArray<NSArray<NSString *> *> *signatures,
|
|
85
|
+
NSString *command, NSArray<NSString *> *events) {
|
|
86
|
+
RemoveManagedHooks(config, signatures, NO);
|
|
87
|
+
CCPetsJSONObject *hooks = config[@"hooks"];
|
|
88
|
+
for (NSString *event in events) {
|
|
89
|
+
NSMutableArray *groups = [hooks[event] isKindOfClass:NSArray.class] ? [hooks[event] mutableCopy] : [NSMutableArray array];
|
|
90
|
+
CCPetsJSONObject *handler = [CCPetsJSONObject new];
|
|
91
|
+
handler[@"type"] = @"command";
|
|
92
|
+
handler[@"command"] = command;
|
|
93
|
+
handler[@"timeout"] = @3;
|
|
94
|
+
CCPetsJSONObject *group = [CCPetsJSONObject new];
|
|
95
|
+
group[@"hooks"] = @[handler];
|
|
96
|
+
[groups addObject:group];
|
|
97
|
+
hooks[event] = groups;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#pragma mark - Claude status line
|
|
102
|
+
|
|
103
|
+
static NSString *FirstCapture(NSString *pattern, NSRegularExpressionOptions options, NSString *text,
|
|
104
|
+
NSInteger group, NSTextCheckingResult **matchOut) {
|
|
105
|
+
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:options error:nil];
|
|
106
|
+
NSTextCheckingResult *match = [expression firstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
|
|
107
|
+
if (matchOut) *matchOut = match;
|
|
108
|
+
if (!match) return nil;
|
|
109
|
+
NSRange range = [match rangeAtIndex:group];
|
|
110
|
+
return range.location == NSNotFound ? @"" : [text substringWithRange:range];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static NSString *Unquote(NSString *value) {
|
|
114
|
+
NSString *inner = FirstCapture(@"\\A(['\"])(.*)\\1\\z", NSRegularExpressionDotMatchesLineSeparators, value, 2, NULL);
|
|
115
|
+
return inner ?: value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static NSString *Trim(NSString *value) {
|
|
119
|
+
return [value stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 能就地插 prelude 的 status line 命令:一个脚本的绝对路径(可带 `~/`),或者
|
|
123
|
+
// `bash ~/.claude/statusline-command.sh` 这类带解释器前缀的写法。带 -c 的不算:
|
|
124
|
+
// `-c` 后面是命令串而不是脚本路径。
|
|
125
|
+
//
|
|
126
|
+
// 返回 nil 表示无法注入;否则 script 是展开后用于读写的绝对路径,target 保留用户原来的写法
|
|
127
|
+
// (含 `~/`,官方默认就是 `~/.claude/statusline.sh`,不能展开后写回配置),
|
|
128
|
+
// interpreter / flags 表示原命令是否带解释器前缀、前缀上是否带选项。
|
|
129
|
+
NSDictionary<NSString *, id> *CCPetsParseStatusLineCommand(id value) {
|
|
130
|
+
if (![value isKindOfClass:NSString.class]) return nil;
|
|
131
|
+
NSString *candidate = Unquote(Trim(value));
|
|
132
|
+
BOOL hasInterpreter = NO;
|
|
133
|
+
NSString *flags = @"";
|
|
134
|
+
NSTextCheckingResult *match = nil;
|
|
135
|
+
FirstCapture(@"\\A(?:/usr/bin/env\\s+)?(?:[^\\s'\"]*/)?(?:ba|z|k|da)?sh((?:\\s+-[A-Za-z]+)*)\\s+(.+)\\z",
|
|
136
|
+
NSRegularExpressionDotMatchesLineSeparators, candidate, 0, &match);
|
|
137
|
+
if (match) {
|
|
138
|
+
NSString *matchedFlags = [candidate substringWithRange:[match rangeAtIndex:1]];
|
|
139
|
+
BOOL runsCommandString = FirstCapture(@"-[A-Za-z]*c(?=\\s|\\z)", 0, matchedFlags, 0, NULL) != nil;
|
|
140
|
+
if (!runsCommandString) {
|
|
141
|
+
hasInterpreter = YES;
|
|
142
|
+
flags = Trim(matchedFlags);
|
|
143
|
+
candidate = Unquote(Trim([candidate substringWithRange:[match rangeAtIndex:2]]));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
NSString *resolved = candidate;
|
|
147
|
+
if ([resolved hasPrefix:@"~/"]) {
|
|
148
|
+
resolved = [CCPetsHomeDirectory() stringByAppendingPathComponent:[resolved substringFromIndex:2]];
|
|
149
|
+
}
|
|
150
|
+
if (![resolved hasPrefix:@"/"]) return nil;
|
|
151
|
+
if (FirstCapture(@"[\\s;&|<>`$()]", 0, resolved, 0, NULL)) return nil;
|
|
152
|
+
return @{@"script": CCPetsResolvePath(resolved), @"target": candidate,
|
|
153
|
+
@"interpreter": @(hasInterpreter), @"flags": @(flags.length > 0)};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static NSString *ReplacePattern(NSString *text, NSString *pattern, NSString *replacement) {
|
|
157
|
+
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
|
|
158
|
+
return [expression stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, text.length)
|
|
159
|
+
withTemplate:[NSRegularExpression escapedTemplateForString:replacement]];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static NSString *RemoveStatusLineBlock(NSString *text) {
|
|
163
|
+
NSString *block = [NSString stringWithFormat:@"(?:\\A|\\n)%@\\n[\\s\\S]*?\\n%@(?=\\n|\\z)",
|
|
164
|
+
[NSRegularExpression escapedPatternForString:CCPetsStatusLineStartMarker],
|
|
165
|
+
[NSRegularExpression escapedPatternForString:CCPetsStatusLineEndMarker]];
|
|
166
|
+
text = ReplacePattern(text, block, @"");
|
|
167
|
+
text = ReplacePattern(text, @"\\A\\n+", @"");
|
|
168
|
+
return ReplacePattern(text, @"\\n{3,}", @"\n\n");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// prelude 把 Claude 喂给 status line 的 JSON 先存一份,交给 cc-pets --claude-usage 记录额度,
|
|
172
|
+
// 再把 stdin 换回这份内容,原脚本照常读到完整输入。
|
|
173
|
+
static NSString *StatusLinePrelude(NSString *binary) {
|
|
174
|
+
NSString *quoted = CCPetsShellQuote(binary);
|
|
175
|
+
return [@[
|
|
176
|
+
CCPetsStatusLineStartMarker,
|
|
177
|
+
@"CC_PETS_STATUSLINE_INPUT=\"$(/usr/bin/mktemp \"${TMPDIR:-/tmp}/cc-pets-statusline.XXXXXX\")\" || exit 1",
|
|
178
|
+
@"/bin/cat > \"${CC_PETS_STATUSLINE_INPUT}\"",
|
|
179
|
+
[NSString stringWithFormat:@"if [ -x %@ ]; then", quoted],
|
|
180
|
+
[NSString stringWithFormat:@" %@ --claude-usage < \"${CC_PETS_STATUSLINE_INPUT}\" >/dev/null 2>&1 || true", quoted],
|
|
181
|
+
@"fi",
|
|
182
|
+
@"exec 0< \"${CC_PETS_STATUSLINE_INPUT}\"",
|
|
183
|
+
@"/bin/rm -f \"${CC_PETS_STATUSLINE_INPUT}\"",
|
|
184
|
+
@"unset CC_PETS_STATUSLINE_INPUT",
|
|
185
|
+
CCPetsStatusLineEndMarker
|
|
186
|
+
] componentsJoinedByString:@"\n"];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 返回是否插入成功;hadShebang 记录原脚本有没有 shebang,决定能不能去掉 command 里的解释器前缀。
|
|
190
|
+
static BOOL InstallStatusLinePrelude(NSString *scriptPath, NSString *binary, BOOL createIfMissing, BOOL *hadShebang) {
|
|
191
|
+
*hadShebang = NO;
|
|
192
|
+
NSString *original = nil;
|
|
193
|
+
mode_t mode = 0700;
|
|
194
|
+
struct stat info;
|
|
195
|
+
if (stat(scriptPath.fileSystemRepresentation, &info) == 0) {
|
|
196
|
+
if (!S_ISREG(info.st_mode)) return NO;
|
|
197
|
+
NSData *data = [NSData dataWithContentsOfFile:scriptPath];
|
|
198
|
+
original = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
|
|
199
|
+
if (!original) return NO;
|
|
200
|
+
mode = info.st_mode & 07777;
|
|
201
|
+
} else if (createIfMissing) {
|
|
202
|
+
[NSFileManager.defaultManager createDirectoryAtPath:scriptPath.stringByDeletingLastPathComponent
|
|
203
|
+
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
204
|
+
original = [NSString stringWithFormat:@"#!/bin/zsh\n%@\n", CCPetsCreatedStatusLineMarker];
|
|
205
|
+
} else {
|
|
206
|
+
return NO;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
NSString *cleaned = RemoveStatusLineBlock(original);
|
|
210
|
+
BOOL hasShebang = [cleaned hasPrefix:@"#!"];
|
|
211
|
+
NSRange newline = [cleaned rangeOfString:@"\n"];
|
|
212
|
+
NSString *shebang = @"#!/bin/zsh";
|
|
213
|
+
NSString *body = cleaned;
|
|
214
|
+
if (hasShebang) {
|
|
215
|
+
shebang = newline.location == NSNotFound ? cleaned : [cleaned substringToIndex:newline.location];
|
|
216
|
+
body = newline.location == NSNotFound ? @"" : [cleaned substringFromIndex:newline.location + 1];
|
|
217
|
+
}
|
|
218
|
+
NSString *updated = [NSString stringWithFormat:@"%@\n%@\n%@", shebang, StatusLinePrelude(binary), body];
|
|
219
|
+
// 结尾的换行收成恰好一个。不能用 \n*\z 全局替换:它在末尾还会再匹配一次空串,多出一行。
|
|
220
|
+
while ([updated hasSuffix:@"\n"]) updated = [updated substringToIndex:updated.length - 1];
|
|
221
|
+
updated = [updated stringByAppendingString:@"\n"];
|
|
222
|
+
if (!CCPetsWriteFileAtomically(scriptPath, updated, mode | 0100, NULL)) return NO;
|
|
223
|
+
*hadShebang = hasShebang;
|
|
224
|
+
return YES;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 包装器形态的 command 末尾是 base64 编码的原命令;Buffer.from 容忍缺失的填充,这里补齐后再解。
|
|
228
|
+
static NSString *DecodeWrappedStatusLine(NSString *command) {
|
|
229
|
+
NSString *encoded = FirstCapture(@"'([A-Za-z0-9+/=]*)'\\s*\\z", 0, command, 1, NULL);
|
|
230
|
+
if (!encoded) return @"";
|
|
231
|
+
encoded = [encoded stringByReplacingOccurrencesOfString:@"=" withString:@""];
|
|
232
|
+
while (encoded.length % 4 != 0) encoded = [encoded stringByAppendingString:@"="];
|
|
233
|
+
NSData *data = [[NSData alloc] initWithBase64EncodedString:encoded options:0];
|
|
234
|
+
if (!data) return @"";
|
|
235
|
+
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?:
|
|
236
|
+
[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] ?: @"";
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
BOOL CCPetsIsManagedStatusLine(NSString *command, NSString *marker) {
|
|
240
|
+
return [command hasPrefix:[marker stringByAppendingString:@" "]] &&
|
|
241
|
+
FirstCapture(@"/bin/claude-statusline-with-pet' '[A-Za-z0-9+/=]*'\\z", 0, command, 0, NULL) != nil;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#pragma mark - 安装入口
|
|
245
|
+
|
|
246
|
+
NSArray<NSString *> *CCPetsClaudeIntegrationFilesAtHome(NSString *claudeHome) {
|
|
247
|
+
NSString *settings = [claudeHome stringByAppendingPathComponent:@"settings.json"];
|
|
248
|
+
CCPetsJSONObject *config = LoadConfig(settings, NULL);
|
|
249
|
+
CCPetsJSONObject *statusLine = config[@"statusLine"];
|
|
250
|
+
NSString *command = [statusLine isKindOfClass:CCPetsJSONObject.class] &&
|
|
251
|
+
[statusLine[@"command"] isKindOfClass:NSString.class] ? statusLine[@"command"] : @"";
|
|
252
|
+
if (CCPetsIsManagedStatusLine(command, CCPetsStatusLineMarker) ||
|
|
253
|
+
CCPetsIsManagedStatusLine(command, CCPetsLegacyStatusLineMarker)) command = DecodeWrappedStatusLine(command);
|
|
254
|
+
if (command.length == 0) command = [claudeHome stringByAppendingPathComponent:@"statusline-command.sh"];
|
|
255
|
+
NSString *script = CCPetsParseStatusLineCommand(command)[@"script"];
|
|
256
|
+
return script.length > 0 ? @[settings, script] : @[settings];
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
int InstallClaudeHooksAtHome(NSString *binary, NSString *claudeHome) {
|
|
260
|
+
if (binary.length == 0) {
|
|
261
|
+
CCPetsPrintLine(stderr, L(@"Usage: cc-pets --install-claude-hooks /absolute/path/to/cc-pets"));
|
|
262
|
+
return 2;
|
|
263
|
+
}
|
|
264
|
+
binary = CCPetsResolvePath(binary);
|
|
265
|
+
claudeHome = CCPetsResolvePath(claudeHome);
|
|
266
|
+
NSString *settingsPath = [claudeHome stringByAppendingPathComponent:@"settings.json"];
|
|
267
|
+
[NSFileManager.defaultManager createDirectoryAtPath:claudeHome withIntermediateDirectories:YES attributes:nil error:nil];
|
|
268
|
+
NSString *errorMessage = nil;
|
|
269
|
+
CCPetsJSONObject *config = LoadConfig(settingsPath, &errorMessage);
|
|
270
|
+
if (!config) {
|
|
271
|
+
CCPetsPrintLine(stderr, [NSString stringWithFormat:L(@"Can't parse %1$@: %2$@"), settingsPath, errorMessage]);
|
|
272
|
+
return 1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
NSDictionary *registered = CCPetsProfileForHome(CCPetsProfileProviderClaude, claudeHome);
|
|
276
|
+
NSString *profileID = registered[@"id"] ?: CCPetsProfileID(CCPetsProfileProviderClaude, claudeHome);
|
|
277
|
+
NSString *profileName = registered[@"displayName"] ?: CCPetsProfileDefaultDisplayName(CCPetsProfileProviderClaude, claudeHome);
|
|
278
|
+
NSString *command = [NSString stringWithFormat:@"%@ CC_PETS_PROFILE_ID=%@ CC_PETS_PROFILE_NAME=%@ CC_PETS_PROFILE_HOME=%@ %@ --hook",
|
|
279
|
+
CCPetsClaudeHookMarker, CCPetsShellQuote(profileID), CCPetsShellQuote(profileName),
|
|
280
|
+
CCPetsShellQuote(claudeHome), CCPetsShellQuote(binary)];
|
|
281
|
+
ReplaceManagedHooks(config, @[@[CCPetsClaudeHookMarker, @"cc-pets"], @[CCPetsLegacyClaudeHookMarker, @"codex-pet"]], command, @[
|
|
282
|
+
@"SessionStart", @"UserPromptSubmit", @"PreToolUse", @"PermissionRequest", @"PostToolUse",
|
|
283
|
+
@"PostToolUseFailure", @"Notification", @"SubagentStart", @"SubagentStop", @"TaskCreated",
|
|
284
|
+
@"TaskCompleted", @"Stop", @"StopFailure", @"SessionEnd"
|
|
285
|
+
]);
|
|
286
|
+
|
|
287
|
+
// 找出用户原本的 status line 命令。已经被我们包装过的,从 base64 里还原出原命令。
|
|
288
|
+
NSString *originalCommand = @"";
|
|
289
|
+
CCPetsJSONObject *statusLine = config[@"statusLine"];
|
|
290
|
+
if ([statusLine isKindOfClass:CCPetsJSONObject.class]) {
|
|
291
|
+
NSString *current = [statusLine[@"command"] isKindOfClass:NSString.class] ? statusLine[@"command"] : @"";
|
|
292
|
+
if (CCPetsIsManagedStatusLine(current, CCPetsStatusLineMarker) || CCPetsIsManagedStatusLine(current, CCPetsLegacyStatusLineMarker)) {
|
|
293
|
+
originalCommand = DecodeWrappedStatusLine(current);
|
|
294
|
+
} else {
|
|
295
|
+
originalCommand = current;
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
statusLine = [CCPetsJSONObject new];
|
|
299
|
+
statusLine[@"type"] = @"command";
|
|
300
|
+
}
|
|
301
|
+
BOOL createdStatusLine = NO;
|
|
302
|
+
if (originalCommand.length == 0) {
|
|
303
|
+
originalCommand = [claudeHome stringByAppendingPathComponent:@"statusline-command.sh"];
|
|
304
|
+
createdStatusLine = YES;
|
|
305
|
+
}
|
|
306
|
+
statusLine = [statusLine copy];
|
|
307
|
+
statusLine[@"type"] = @"command";
|
|
308
|
+
statusLine[@"command"] = originalCommand;
|
|
309
|
+
config[@"statusLine"] = statusLine;
|
|
310
|
+
|
|
311
|
+
NSDictionary<NSString *, id> *parsed = CCPetsParseStatusLineCommand(originalCommand);
|
|
312
|
+
BOOL hadShebang = NO;
|
|
313
|
+
BOOL statusLineInstalled = parsed &&
|
|
314
|
+
InstallStatusLinePrelude(parsed[@"script"], binary, createdStatusLine, &hadShebang);
|
|
315
|
+
|
|
316
|
+
// 只在原命令带解释器前缀(`bash ~/x.sh`)时才改写 command,去掉那层前缀让 agent 能直接读懂。
|
|
317
|
+
// 纯路径形态——包括官方默认的 `~/.claude/statusline.sh`——一个字都不动。另外两道闸门都是为了
|
|
318
|
+
// 不改变脚本的执行语义:带 flag(`sh -e ~/x.sh`)不动,否则 -e 会被悄悄丢掉;原脚本没有
|
|
319
|
+
// shebang 的不动,否则 prelude 补上的 #!/bin/zsh 会把本来用 bash 跑的脚本换成 zsh。
|
|
320
|
+
if (statusLineInstalled && [parsed[@"interpreter"] boolValue] && ![parsed[@"flags"] boolValue] && hadShebang) {
|
|
321
|
+
statusLine[@"command"] = parsed[@"target"];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// 解析不出脚本路径的命令(`bun x ccstatusline`、`npx …`、带管道或 `sh -c` 的命令行)没法就地插
|
|
325
|
+
// prelude,而 status line 是 Claude 额度的唯一来源。回退到包装器:command 换成
|
|
326
|
+
// claude-statusline-with-pet + base64 编码的原命令,它先把 stdin 喂给 cc-pets --claude-usage,
|
|
327
|
+
// 再原样转发给原命令执行。卸载侧认这个形态,能原样还原。
|
|
328
|
+
NSString *packageDirectory = binary.stringByDeletingLastPathComponent
|
|
329
|
+
.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent;
|
|
330
|
+
NSString *wrapper = [packageDirectory stringByAppendingPathComponent:@"bin/claude-statusline-with-pet"];
|
|
331
|
+
BOOL statusLineWrapped = NO;
|
|
332
|
+
struct stat wrapperInfo;
|
|
333
|
+
if (!statusLineInstalled && stat(wrapper.fileSystemRepresentation, &wrapperInfo) == 0) {
|
|
334
|
+
// 包内文件不可写(只读安装)时权限位通常本来就是对的,失败不影响。
|
|
335
|
+
chmod(wrapper.fileSystemRepresentation, (wrapperInfo.st_mode & 07777) | 0100);
|
|
336
|
+
NSString *encoded = [[originalCommand dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
|
|
337
|
+
statusLine[@"type"] = @"command";
|
|
338
|
+
statusLine[@"command"] = [NSString stringWithFormat:@"%@ %@ '%@'", CCPetsStatusLineMarker, CCPetsShellQuote(wrapper), encoded];
|
|
339
|
+
statusLineWrapped = YES;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (!statusLineInstalled && !statusLineWrapped) {
|
|
343
|
+
CCPetsPrintLine(stderr, [NSString stringWithFormat:L(@"Claude status line not hooked up: %@"), originalCommand]);
|
|
344
|
+
CCPetsPrintLine(stderr, L(@"Claude hooks still work, but the pet can't show Claude quota % (token usage is unaffected)."));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
NSString *text = [CCPetsJSONStringify(config) stringByAppendingString:@"\n"];
|
|
348
|
+
if (!CCPetsWriteFileAtomically(settingsPath, text, 0600, &errorMessage)) {
|
|
349
|
+
CCPetsPrintLine(stderr, errorMessage);
|
|
350
|
+
return 1;
|
|
351
|
+
}
|
|
352
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"Claude Code hooks installed in %@"), settingsPath]);
|
|
353
|
+
if (statusLineInstalled) {
|
|
354
|
+
CCPetsPrintLine(stdout, L(@"Claude quota tracking added to your existing status line. The pet shows quota after the first API response."));
|
|
355
|
+
} else if (statusLineWrapped) {
|
|
356
|
+
CCPetsPrintLine(stdout, L(@"Claude quota tracking added to the status line through a wrapper; your original command still runs. The pet shows quota after the first API response."));
|
|
357
|
+
}
|
|
358
|
+
return 0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
int InstallClaudeHooks(NSString *binary) {
|
|
362
|
+
if (!CCPetsClaudeCLIInstalled()) {
|
|
363
|
+
CCPetsPrintLine(stdout, L(@"Claude Code CLI not detected; skipping Claude hooks and status line setup."));
|
|
364
|
+
CCPetsPrintLine(stdout, L(@"If you install Claude Code later, run cc-pets install to set it up."));
|
|
365
|
+
return 0;
|
|
366
|
+
}
|
|
367
|
+
return InstallClaudeHooksAtHome(binary, CCPetsClaudeHome());
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
int InstallCodexHooksAtHome(NSString *binary, NSString *codexHome) {
|
|
371
|
+
if (binary.length == 0) {
|
|
372
|
+
CCPetsPrintLine(stderr, L(@"Usage: cc-pets --install-codex-hooks /absolute/path/to/cc-pets"));
|
|
373
|
+
return 2;
|
|
374
|
+
}
|
|
375
|
+
binary = CCPetsResolvePath(binary);
|
|
376
|
+
codexHome = CCPetsResolvePath(codexHome);
|
|
377
|
+
NSString *hooksPath = [codexHome stringByAppendingPathComponent:@"hooks.json"];
|
|
378
|
+
[NSFileManager.defaultManager createDirectoryAtPath:codexHome withIntermediateDirectories:YES attributes:nil error:nil];
|
|
379
|
+
NSString *errorMessage = nil;
|
|
380
|
+
CCPetsJSONObject *config = LoadConfig(hooksPath, &errorMessage);
|
|
381
|
+
if (!config) {
|
|
382
|
+
CCPetsPrintLine(stderr, [NSString stringWithFormat:L(@"Can't parse %1$@: %2$@"), hooksPath, errorMessage]);
|
|
383
|
+
return 1;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
NSDictionary *registered = CCPetsProfileForHome(CCPetsProfileProviderCodex, codexHome);
|
|
387
|
+
NSString *profileID = registered[@"id"] ?: CCPetsProfileID(CCPetsProfileProviderCodex, codexHome);
|
|
388
|
+
NSString *profileName = registered[@"displayName"] ?: CCPetsProfileDefaultDisplayName(CCPetsProfileProviderCodex, codexHome);
|
|
389
|
+
NSString *command = [NSString stringWithFormat:@"%@ CC_PETS_PROFILE_ID=%@ CC_PETS_PROFILE_NAME=%@ CC_PETS_PROFILE_HOME=%@ %@ --hook",
|
|
390
|
+
CCPetsCodexHookMarker, CCPetsShellQuote(profileID), CCPetsShellQuote(profileName),
|
|
391
|
+
CCPetsShellQuote(codexHome), CCPetsShellQuote(binary)];
|
|
392
|
+
ReplaceManagedHooks(config, @[@[CCPetsCodexHookMarker, @"cc-pets"], @[CCPetsLegacyCodexHookMarker, @"codex-pet"]], command, @[
|
|
393
|
+
@"SessionStart", @"UserPromptSubmit", @"PreToolUse", @"PermissionRequest", @"PostToolUse",
|
|
394
|
+
@"SubagentStart", @"SubagentStop", @"Stop"
|
|
395
|
+
]);
|
|
396
|
+
|
|
397
|
+
NSString *text = [CCPetsJSONStringify(config) stringByAppendingString:@"\n"];
|
|
398
|
+
if (!CCPetsWriteFileAtomically(hooksPath, text, 0600, &errorMessage)) {
|
|
399
|
+
CCPetsPrintLine(stderr, errorMessage);
|
|
400
|
+
return 1;
|
|
401
|
+
}
|
|
402
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"Codex agent hooks installed in %@"), hooksPath]);
|
|
403
|
+
CCPetsPrintLine(stdout, L(@"Next time you start Codex, run /hooks and trust the CC Pets hooks."));
|
|
404
|
+
return 0;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
int InstallCodexHooks(NSString *binary) {
|
|
408
|
+
if (!CCPetsCodexCLIInstalled()) {
|
|
409
|
+
CCPetsPrintLine(stdout, L(@"Codex CLI not detected; skipping Codex hooks."));
|
|
410
|
+
CCPetsPrintLine(stdout, L(@"If you install Codex later, run cc-pets install to set it up."));
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
return InstallCodexHooksAtHome(binary, CCPetsCodexHome());
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#pragma mark - 卸载入口
|
|
417
|
+
|
|
418
|
+
NSString *const CCPetsShimStartMarker = @"# >>> cc-pets-shims >>>";
|
|
419
|
+
NSString *const CCPetsShimEndMarker = @"# <<< cc-pets-shims <<<";
|
|
420
|
+
|
|
421
|
+
// 去掉脚本里的 prelude。脚本本来就是安装器新建的(去掉之后只剩创建标记),直接删掉并通过
|
|
422
|
+
// created 告诉调用方,status line 配置也要一起删。返回脚本是否有变化。
|
|
423
|
+
static BOOL RemoveStatusLinePrelude(NSString *scriptPath, BOOL *created) {
|
|
424
|
+
*created = NO;
|
|
425
|
+
struct stat info;
|
|
426
|
+
if (scriptPath.length == 0 || stat(scriptPath.fileSystemRepresentation, &info) != 0 || !S_ISREG(info.st_mode)) return NO;
|
|
427
|
+
NSData *data = [NSData dataWithContentsOfFile:scriptPath];
|
|
428
|
+
NSString *original = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
|
|
429
|
+
if (!original) return NO;
|
|
430
|
+
NSString *updated = RemoveStatusLineBlock(original);
|
|
431
|
+
while ([updated hasSuffix:@"\n"]) updated = [updated substringToIndex:updated.length - 1];
|
|
432
|
+
updated = [updated stringByAppendingString:@"\n"];
|
|
433
|
+
if ([updated isEqualToString:original]) return NO;
|
|
434
|
+
if ([updated isEqualToString:[NSString stringWithFormat:@"#!/bin/zsh\n%@\n", CCPetsCreatedStatusLineMarker]]) {
|
|
435
|
+
unlink(scriptPath.fileSystemRepresentation);
|
|
436
|
+
*created = YES;
|
|
437
|
+
return YES;
|
|
438
|
+
}
|
|
439
|
+
return CCPetsWriteFileAtomically(scriptPath, updated, info.st_mode & 07777, NULL);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// 读改写一份 JSON 配置。文件不存在、顶层不是对象或 updater 没改动时都不写。
|
|
443
|
+
// 解析失败只报错不写,*failed 置位,其余集成照常清理。
|
|
444
|
+
static BOOL UpdateConfig(NSString *path, BOOL *failed, BOOL (^updater)(CCPetsJSONObject *config)) {
|
|
445
|
+
NSString *errorMessage = nil;
|
|
446
|
+
id config = CCPetsReadConfig(path, &errorMessage);
|
|
447
|
+
if (!config) {
|
|
448
|
+
if (errorMessage) {
|
|
449
|
+
CCPetsPrintLine(stderr, [NSString stringWithFormat:L(@"Can't parse %1$@, left unchanged: %2$@"), path, errorMessage]);
|
|
450
|
+
*failed = YES;
|
|
451
|
+
}
|
|
452
|
+
return NO;
|
|
453
|
+
}
|
|
454
|
+
if (![config isKindOfClass:CCPetsJSONObject.class] || !updater(config)) return NO;
|
|
455
|
+
NSString *text = [CCPetsJSONStringify(config) stringByAppendingString:@"\n"];
|
|
456
|
+
if (!CCPetsWriteFileAtomically(path, text, 0600, &errorMessage)) {
|
|
457
|
+
CCPetsPrintLine(stderr, errorMessage);
|
|
458
|
+
*failed = YES;
|
|
459
|
+
return NO;
|
|
460
|
+
}
|
|
461
|
+
return YES;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
static NSString *RemoveMarkedBlock(NSString *text, NSString *startMarker, NSString *endMarker) {
|
|
465
|
+
NSString *pattern = [NSString stringWithFormat:@"(?:\\A|\\n)%@\\n[\\s\\S]*?%@(?=\\n|\\z)",
|
|
466
|
+
[NSRegularExpression escapedPatternForString:startMarker],
|
|
467
|
+
[NSRegularExpression escapedPatternForString:endMarker]];
|
|
468
|
+
return ReplacePattern(text, pattern, @"");
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// .zshrc 里的段落。cc-pets-codex / cc-pets-claude 是 alias 时代的写法:alias 区分大小写,挡不住
|
|
472
|
+
// Claude / CODEX,已被 cc-pets-shims 的 PATH shim 取代;安装前的清理也会走这里,顺带迁移掉。
|
|
473
|
+
static BOOL RemoveShellIntegration(NSString *shellRC) {
|
|
474
|
+
struct stat info;
|
|
475
|
+
if (shellRC.length == 0 || stat(shellRC.fileSystemRepresentation, &info) != 0) return NO;
|
|
476
|
+
NSData *data = [NSData dataWithContentsOfFile:shellRC];
|
|
477
|
+
NSString *original = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
|
|
478
|
+
if (!original) return NO;
|
|
479
|
+
NSString *updated = RemoveMarkedBlock(original, CCPetsShimStartMarker, CCPetsShimEndMarker);
|
|
480
|
+
for (NSString *name in @[@"cc-pets-codex", @"cc-pets-claude", @"codex-pet", @"claude-pet"]) {
|
|
481
|
+
updated = RemoveMarkedBlock(updated, [NSString stringWithFormat:@"# >>> %@ >>>", name],
|
|
482
|
+
[NSString stringWithFormat:@"# <<< %@ <<<", name]);
|
|
483
|
+
}
|
|
484
|
+
updated = ReplacePattern(updated, @"\\n{3,}", @"\n\n");
|
|
485
|
+
if ([updated isEqualToString:original]) return NO;
|
|
486
|
+
return CCPetsWriteFileAtomically(shellRC, updated, info.st_mode & 07777, NULL);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// 只删指向 bin/<name>-with-pet 的软链,别人放进来的同名文件不动;目录空了再删目录。
|
|
490
|
+
static BOOL RemoveShims(void) {
|
|
491
|
+
NSString *directory = CCPetsEnvironmentValue(@"CC_PETS_SHIM_DIR") ?:
|
|
492
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@".cc-pets/shims"];
|
|
493
|
+
if (![NSFileManager.defaultManager fileExistsAtPath:directory]) return NO;
|
|
494
|
+
BOOL changed = NO;
|
|
495
|
+
for (NSString *name in @[@"codex", @"claude"]) {
|
|
496
|
+
NSString *shim = [directory stringByAppendingPathComponent:name];
|
|
497
|
+
// lstat:目标包被 npm uninstall 删掉后软链是断的,但仍然要清掉。
|
|
498
|
+
struct stat info;
|
|
499
|
+
if (lstat(shim.fileSystemRepresentation, &info) != 0 || !S_ISLNK(info.st_mode)) continue;
|
|
500
|
+
NSString *target = [NSFileManager.defaultManager destinationOfSymbolicLinkAtPath:shim error:nil];
|
|
501
|
+
if (![target hasSuffix:[NSString stringWithFormat:@"/bin/%@-with-pet", name]]) continue;
|
|
502
|
+
if (unlink(shim.fileSystemRepresentation) == 0) changed = YES;
|
|
503
|
+
}
|
|
504
|
+
NSArray *remaining = [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil];
|
|
505
|
+
if (remaining && remaining.count == 0) rmdir(directory.fileSystemRepresentation);
|
|
506
|
+
return changed;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
static BOOL RemoveCodexHooksAtHome(NSString *codexHome, BOOL preparingInstall, BOOL *failed) {
|
|
510
|
+
NSArray *signatures = preparingInstall
|
|
511
|
+
? @[@[CCPetsLegacyCodexHookMarker, @"codex-pet"]]
|
|
512
|
+
: @[@[CCPetsCodexHookMarker, @"cc-pets"], @[CCPetsLegacyCodexHookMarker, @"codex-pet"]];
|
|
513
|
+
NSString *path = [codexHome stringByAppendingPathComponent:@"hooks.json"];
|
|
514
|
+
return UpdateConfig(path, failed, ^BOOL(CCPetsJSONObject *config) {
|
|
515
|
+
return RemoveManagedHooks(config, signatures, YES);
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
static BOOL RemoveClaudeHooksAtHome(NSString *claudeHome, BOOL preparingInstall, BOOL *failed) {
|
|
520
|
+
NSArray *signatures = preparingInstall
|
|
521
|
+
? @[@[CCPetsLegacyClaudeHookMarker, @"codex-pet"]]
|
|
522
|
+
: @[@[CCPetsClaudeHookMarker, @"cc-pets"], @[CCPetsLegacyClaudeHookMarker, @"codex-pet"]];
|
|
523
|
+
NSArray<NSString *> *statusLineMarkers = preparingInstall
|
|
524
|
+
? @[CCPetsLegacyStatusLineMarker] : @[CCPetsStatusLineMarker, CCPetsLegacyStatusLineMarker];
|
|
525
|
+
NSString *path = [claudeHome stringByAppendingPathComponent:@"settings.json"];
|
|
526
|
+
return UpdateConfig(path, failed, ^BOOL(CCPetsJSONObject *config) {
|
|
527
|
+
BOOL changed = RemoveManagedHooks(config, signatures, YES);
|
|
528
|
+
CCPetsJSONObject *statusLine = config[@"statusLine"];
|
|
529
|
+
BOOL isObject = [statusLine isKindOfClass:CCPetsJSONObject.class];
|
|
530
|
+
NSString *command = isObject && [statusLine[@"command"] isKindOfClass:NSString.class] ? statusLine[@"command"] : @"";
|
|
531
|
+
BOOL wrapped = NO;
|
|
532
|
+
for (NSString *marker in statusLineMarkers) wrapped = wrapped || CCPetsIsManagedStatusLine(command, marker);
|
|
533
|
+
if (wrapped) {
|
|
534
|
+
NSString *originalCommand = DecodeWrappedStatusLine(command);
|
|
535
|
+
if (originalCommand.length > 0) {
|
|
536
|
+
CCPetsJSONObject *restored = [statusLine copy];
|
|
537
|
+
restored[@"command"] = originalCommand;
|
|
538
|
+
config[@"statusLine"] = restored;
|
|
539
|
+
} else {
|
|
540
|
+
[config removeObjectForKey:@"statusLine"];
|
|
541
|
+
}
|
|
542
|
+
changed = YES;
|
|
543
|
+
}
|
|
544
|
+
// 安装前不动注入的 prelude,紧接着的安装会原位替换它。
|
|
545
|
+
if (!preparingInstall) {
|
|
546
|
+
CCPetsJSONObject *current = config[@"statusLine"];
|
|
547
|
+
id currentCommand = [current isKindOfClass:CCPetsJSONObject.class] ? current[@"command"] : nil;
|
|
548
|
+
BOOL created = NO;
|
|
549
|
+
if (RemoveStatusLinePrelude(CCPetsParseStatusLineCommand(currentCommand)[@"script"], &created)) changed = YES;
|
|
550
|
+
if (created) [config removeObjectForKey:@"statusLine"];
|
|
551
|
+
}
|
|
552
|
+
return changed;
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
int UninstallClaudeHooksAtHome(NSString *claudeHome) {
|
|
557
|
+
BOOL failed = NO;
|
|
558
|
+
BOOL changed = RemoveClaudeHooksAtHome(CCPetsResolvePath(claudeHome), NO, &failed);
|
|
559
|
+
if (changed) CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"Removed the CC Pets hooks in %@ and restored the original status line."),
|
|
560
|
+
[claudeHome stringByAppendingPathComponent:@"settings.json"]]);
|
|
561
|
+
return failed ? 1 : 0;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
int UninstallCodexHooksAtHome(NSString *codexHome) {
|
|
565
|
+
BOOL failed = NO;
|
|
566
|
+
BOOL changed = RemoveCodexHooksAtHome(CCPetsResolvePath(codexHome), NO, &failed);
|
|
567
|
+
if (changed) CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"Removed the CC Pets hooks in %@."),
|
|
568
|
+
[codexHome stringByAppendingPathComponent:@"hooks.json"]]);
|
|
569
|
+
return failed ? 1 : 0;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
int UninstallIntegrations(BOOL preparingInstall, NSString *shellRC) {
|
|
573
|
+
BOOL failed = NO;
|
|
574
|
+
// 安装/卸载的隐式范围只包含官方默认 Home;环境变量指向的自定义 Home 必须先显式登记。
|
|
575
|
+
NSString *defaultCodexHome = [CCPetsHomeDirectory() stringByAppendingPathComponent:@".codex"];
|
|
576
|
+
NSString *defaultClaudeHome = [CCPetsHomeDirectory() stringByAppendingPathComponent:@".claude"];
|
|
577
|
+
NSString *codexHooksPath = [defaultCodexHome stringByAppendingPathComponent:@"hooks.json"];
|
|
578
|
+
BOOL codexChanged = RemoveCodexHooksAtHome(defaultCodexHome, preparingInstall, &failed);
|
|
579
|
+
NSString *claudeSettingsPath = [defaultClaudeHome stringByAppendingPathComponent:@"settings.json"];
|
|
580
|
+
BOOL claudeChanged = RemoveClaudeHooksAtHome(defaultClaudeHome, preparingInstall, &failed);
|
|
581
|
+
BOOL profileChanged = NO;
|
|
582
|
+
|
|
583
|
+
// 显式登记的 Home 也属于安装范围。默认 Home 已迁移进注册表时按规范化路径去重。
|
|
584
|
+
NSMutableSet<NSString *> *handledHomes = [NSMutableSet setWithObjects:
|
|
585
|
+
CCPetsResolvePath(defaultCodexHome), CCPetsResolvePath(defaultClaudeHome), nil];
|
|
586
|
+
for (NSDictionary *profile in CCPetsProfiles()) {
|
|
587
|
+
NSString *home = CCPetsResolvePath(profile[@"home"]);
|
|
588
|
+
if (home.length == 0 || [handledHomes containsObject:home]) continue;
|
|
589
|
+
[handledHomes addObject:home];
|
|
590
|
+
BOOL changed = [profile[@"provider"] isEqualToString:CCPetsProfileProviderClaude]
|
|
591
|
+
? RemoveClaudeHooksAtHome(home, preparingInstall, &failed)
|
|
592
|
+
: RemoveCodexHooksAtHome(home, preparingInstall, &failed);
|
|
593
|
+
if (changed) {
|
|
594
|
+
profileChanged = YES;
|
|
595
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"%1$@ the CC Pets hooks in %2$@."),
|
|
596
|
+
preparingInstall ? L(@"Migrated") : L(@"Removed"), home]);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
BOOL shellChanged = RemoveShellIntegration(shellRC);
|
|
601
|
+
// shim 只在真正卸载时删:安装前删掉的话,重建失败就会留下一个没有 codex / claude 的 PATH 目录。
|
|
602
|
+
BOOL shimChanged = !preparingInstall && RemoveShims();
|
|
603
|
+
|
|
604
|
+
NSString *action = preparingInstall ? L(@"Migrated") : L(@"Removed");
|
|
605
|
+
if (codexChanged) {
|
|
606
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"%1$@ the CC Pets hooks in %2$@."), action, codexHooksPath]);
|
|
607
|
+
}
|
|
608
|
+
if (claudeChanged) {
|
|
609
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"%1$@ the CC Pets hooks in %2$@ and restored the original status line."),
|
|
610
|
+
action, claudeSettingsPath]);
|
|
611
|
+
}
|
|
612
|
+
if (shellChanged) {
|
|
613
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"%1$@ the CC Pets shell integration in %2$@."), action, shellRC]);
|
|
614
|
+
}
|
|
615
|
+
if (shimChanged) {
|
|
616
|
+
NSString *directory = CCPetsEnvironmentValue(@"CC_PETS_SHIM_DIR") ?:
|
|
617
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@".cc-pets/shims"];
|
|
618
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"%1$@ the codex / claude shims in %2$@."), action, directory]);
|
|
619
|
+
}
|
|
620
|
+
if (!codexChanged && !claudeChanged && !profileChanged && !shellChanged && !shimChanged && !preparingInstall) {
|
|
621
|
+
CCPetsPrintLine(stdout, L(@"No CC Pets integrations to remove."));
|
|
622
|
+
}
|
|
623
|
+
return failed ? 1 : 0;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
#pragma mark - 自动更新配置
|
|
627
|
+
|
|
628
|
+
// 应用内更新走 `node npm-cli.js install -g cc-pets@latest`。npm 装包时把自己用的 node 和 npm-cli
|
|
629
|
+
// 通过 npm_node_execpath / npm_execpath 告诉 postinstall,这里记下真实路径。拿不到时只提示不报错:
|
|
630
|
+
// 自动更新是锦上添花,不能因此让安装失败。
|
|
631
|
+
int ConfigureUpdater(void) {
|
|
632
|
+
NSString *nodeCandidate = CCPetsEnvironmentValue(@"npm_node_execpath") ?: CCPetsFindRealExecutable(@"node", nil);
|
|
633
|
+
NSString *npmCandidate = CCPetsEnvironmentValue(@"npm_execpath");
|
|
634
|
+
if (!nodeCandidate || !npmCandidate) {
|
|
635
|
+
CCPetsPrintLine(stderr, L(@"Auto-update config not saved: npm didn't provide the node/npm-cli paths."));
|
|
636
|
+
return 0;
|
|
637
|
+
}
|
|
638
|
+
NSString *nodePath = CCPetsRealPath(nodeCandidate);
|
|
639
|
+
NSString *npmCliPath = CCPetsRealPath(npmCandidate);
|
|
640
|
+
if (!nodePath || !npmCliPath) {
|
|
641
|
+
CCPetsPrintLine(stderr, L(@"Auto-update config not saved: the node/npm-cli paths don't exist."));
|
|
642
|
+
return 0;
|
|
643
|
+
}
|
|
644
|
+
NSString *supportDirectory = CCPetsEnvironmentValue(@"CC_PETS_APPLICATION_SUPPORT_DIR") ?:
|
|
645
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/CC Pets"];
|
|
646
|
+
NSString *applicationsDirectory = CCPetsEnvironmentValue(@"CC_PETS_APPLICATIONS_DIR") ?:
|
|
647
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@"Applications"];
|
|
648
|
+
[NSFileManager.defaultManager createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES
|
|
649
|
+
attributes:@{NSFilePosixPermissions: @0700} error:nil];
|
|
650
|
+
CCPetsJSONObject *configuration = [CCPetsJSONObject new];
|
|
651
|
+
configuration[@"schemaVersion"] = @2;
|
|
652
|
+
configuration[@"nodePath"] = nodePath;
|
|
653
|
+
configuration[@"npmCliPath"] = npmCliPath;
|
|
654
|
+
configuration[@"appPath"] = [applicationsDirectory stringByAppendingPathComponent:@"CC Pets.app"];
|
|
655
|
+
NSString *error = nil;
|
|
656
|
+
if (!CCPetsWriteFileAtomically([supportDirectory stringByAppendingPathComponent:@"updater.json"],
|
|
657
|
+
[CCPetsJSONStringify(configuration) stringByAppendingString:@"\n"], 0600, &error)) {
|
|
658
|
+
CCPetsPrintLine(stderr, error);
|
|
659
|
+
return 1;
|
|
660
|
+
}
|
|
661
|
+
return 0;
|
|
662
|
+
}
|