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,632 @@
|
|
|
1
|
+
#import "CCPetsL10n.h"
|
|
2
|
+
#import "CCPetsDoctor.h"
|
|
3
|
+
#import "CCPetsBridge.h"
|
|
4
|
+
#import "CCPetsBridgeSetup.h"
|
|
5
|
+
#import "CCPetsBridgeStore.h"
|
|
6
|
+
#import "CCPetsCLISupport.h"
|
|
7
|
+
#import "CCPetsCleanup.h"
|
|
8
|
+
#import "CCPetsIntegrations.h"
|
|
9
|
+
#import "CCPetsJSON.h"
|
|
10
|
+
#import "CCPetsPaths.h"
|
|
11
|
+
#import "CCPetsPhrases.h"
|
|
12
|
+
#import "CCPetsProfiles.h"
|
|
13
|
+
#import "CCPetsVersion.h"
|
|
14
|
+
#import <sys/stat.h>
|
|
15
|
+
#import <sys/utsname.h>
|
|
16
|
+
#import <math.h>
|
|
17
|
+
#import <errno.h>
|
|
18
|
+
|
|
19
|
+
#define LF(localized, values) CCPetsInterpolate(localized, values)
|
|
20
|
+
|
|
21
|
+
// 安装脚本在 App 数据目录里记下 npm 包目录:装到 ~/Applications 的 App 是拷贝,
|
|
22
|
+
// 从自己的位置推不出包在哪。
|
|
23
|
+
static NSString *const PackageDirectoryMarker = @"package-dir";
|
|
24
|
+
|
|
25
|
+
// npm 包目录。三种来源,按可靠程度:
|
|
26
|
+
// 1. 可执行文件在 <包目录>/.build/release/ 之下(命令行二进制,或构建出来的 App);
|
|
27
|
+
// 2. 安装脚本留下的记号文件(~/Applications 里的 App);
|
|
28
|
+
// 3. 退回按命令行二进制的相对位置推算(<包目录>/.build/release/cc-pets)。
|
|
29
|
+
static NSString *PackageDirectory(void) {
|
|
30
|
+
NSString *executable = CCPetsExecutablePath();
|
|
31
|
+
NSRange built = [executable rangeOfString:@"/.build/release/"];
|
|
32
|
+
if (built.location != NSNotFound) return [executable substringToIndex:built.location];
|
|
33
|
+
NSString *marked = [[NSString stringWithContentsOfFile:
|
|
34
|
+
[ApplicationSupportDirectory() stringByAppendingPathComponent:PackageDirectoryMarker]
|
|
35
|
+
encoding:NSUTF8StringEncoding error:nil] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
36
|
+
if (marked.length > 0 && [NSFileManager.defaultManager fileExistsAtPath:
|
|
37
|
+
[marked stringByAppendingPathComponent:@"package.json"]]) return marked;
|
|
38
|
+
return executable.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent
|
|
39
|
+
.stringByDeletingLastPathComponent;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static NSString *PetBinary(void) {
|
|
43
|
+
return [PackageDirectory() stringByAppendingPathComponent:@".build/release/cc-pets"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 版本以 package.json 为准(npm 安装的就是它);读不到时退回编进二进制的版本。
|
|
47
|
+
static NSString *PackageVersion(void) {
|
|
48
|
+
NSData *data = [NSData dataWithContentsOfFile:[PackageDirectory() stringByAppendingPathComponent:@"package.json"]];
|
|
49
|
+
NSDictionary *package = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
50
|
+
NSString *version = [package isKindOfClass:NSDictionary.class] && [package[@"version"] isKindOfClass:NSString.class]
|
|
51
|
+
? package[@"version"] : nil;
|
|
52
|
+
return version ?: @CC_PETS_VERSION;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static NSString *ShellRC(void) {
|
|
56
|
+
return [(CCPetsEnvironmentValue(@"ZDOTDIR") ?: CCPetsHomeDirectory()) stringByAppendingPathComponent:@".zshrc"];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static NSString *InstalledApp(void) {
|
|
60
|
+
return [(CCPetsEnvironmentValue(@"CC_PETS_APPLICATIONS_DIR") ?: [CCPetsHomeDirectory() stringByAppendingPathComponent:@"Applications"])
|
|
61
|
+
stringByAppendingPathComponent:@"CC Pets.app"];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static NSDictionary<NSString *, id> *NativePaths(void) {
|
|
65
|
+
NSMutableDictionary *paths = [CCPetsDataPaths() mutableCopy];
|
|
66
|
+
paths[@"phrases"] = PetPhrasesFilePath();
|
|
67
|
+
paths[@"petPhrases"] = PetPhrasesPetDirectory();
|
|
68
|
+
return paths;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 文件 / 目录占用的字节数,不存在时为 nil。软链按软链本身算,不跟进去。
|
|
72
|
+
static NSNumber *SizeOf(NSString *target) {
|
|
73
|
+
struct stat info;
|
|
74
|
+
if (lstat(target.fileSystemRepresentation, &info) != 0) return nil;
|
|
75
|
+
if (!S_ISDIR(info.st_mode)) return @(info.st_size);
|
|
76
|
+
unsigned long long total = 0;
|
|
77
|
+
for (NSString *entry in [NSFileManager.defaultManager contentsOfDirectoryAtPath:target error:nil]) {
|
|
78
|
+
total += [SizeOf([target stringByAppendingPathComponent:entry]) unsignedLongLongValue];
|
|
79
|
+
}
|
|
80
|
+
return @(total);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static NSString *FormatSize(NSNumber *bytes) {
|
|
84
|
+
if (!bytes) return L(@"missing");
|
|
85
|
+
double value = bytes.doubleValue;
|
|
86
|
+
if (value < 1024) return [NSString stringWithFormat:@"%llu B", bytes.unsignedLongLongValue];
|
|
87
|
+
if (value < 1024 * 1024) return [NSString stringWithFormat:@"%.1f KB", value / 1024];
|
|
88
|
+
return [NSString stringWithFormat:@"%.1f MB", value / 1024 / 1024];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 终端里中文占两列,按字符数对齐会错位。
|
|
92
|
+
static NSUInteger DisplayWidth(NSString *text) {
|
|
93
|
+
NSUInteger width = 0;
|
|
94
|
+
for (NSUInteger index = 0; index < text.length; index++) {
|
|
95
|
+
unichar character = [text characterAtIndex:index];
|
|
96
|
+
BOOL wide = (character >= 0x1100 && character <= 0x115f) || (character >= 0x2e80 && character <= 0xa4cf) ||
|
|
97
|
+
(character >= 0xac00 && character <= 0xd7a3) || (character >= 0xf900 && character <= 0xfaff) ||
|
|
98
|
+
(character >= 0xfe30 && character <= 0xfe4f) || (character >= 0xff00 && character <= 0xff60) ||
|
|
99
|
+
(character >= 0xffe0 && character <= 0xffe6);
|
|
100
|
+
width += wide ? 2 : 1;
|
|
101
|
+
}
|
|
102
|
+
return width;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 读 JSON 配置:@{value} 或 @{error: missing | invalid}。
|
|
106
|
+
static NSDictionary *ReadJSONFile(NSString *file) {
|
|
107
|
+
NSData *data = [NSData dataWithContentsOfFile:file];
|
|
108
|
+
if (!data) return @{@"error": [NSFileManager.defaultManager fileExistsAtPath:file] ? @"invalid" : @"missing"};
|
|
109
|
+
id value = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingFragmentsAllowed error:nil];
|
|
110
|
+
return value ? @{@"value": value} : @{@"error": @"invalid"};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#pragma mark - cc-pets paths
|
|
114
|
+
|
|
115
|
+
static NSArray<NSDictionary *> *PathGroups(NSDictionary *native) {
|
|
116
|
+
NSMutableArray *integrationItems = [NSMutableArray arrayWithArray:@[
|
|
117
|
+
@[L(@"npm package"), PackageDirectory()],
|
|
118
|
+
@[L(@"Installed app"), InstalledApp()],
|
|
119
|
+
@[L(@"Agent profile registry"), CCPetsProfilesPath()],
|
|
120
|
+
@[L(@"Claude Code settings"), [[CCPetsHomeDirectory() stringByAppendingPathComponent:@".claude"]
|
|
121
|
+
stringByAppendingPathComponent:@"settings.json"]],
|
|
122
|
+
@[@"Codex Hooks", [[CCPetsHomeDirectory() stringByAppendingPathComponent:@".codex"]
|
|
123
|
+
stringByAppendingPathComponent:@"hooks.json"]],
|
|
124
|
+
@[L(@"Shell config"), ShellRC()]
|
|
125
|
+
]];
|
|
126
|
+
for (NSDictionary *profile in CCPetsProfiles()) {
|
|
127
|
+
NSString *name = profile[@"displayName"] ?: profile[@"slug"] ?: profile[@"provider"];
|
|
128
|
+
NSString *file = [profile[@"home"] stringByAppendingPathComponent:
|
|
129
|
+
[profile[@"provider"] isEqualToString:CCPetsProfileProviderClaude] ? @"settings.json" : @"hooks.json"];
|
|
130
|
+
[integrationItems addObject:@[[NSString stringWithFormat:L(@"Agent profile: %@"), name], file]];
|
|
131
|
+
}
|
|
132
|
+
return @[
|
|
133
|
+
@{@"title": L(@"Settings (removed by --purge)"), @"items": @[@[L(@"Preferences"), native[@"preferences"]]]},
|
|
134
|
+
@{@"title": L(@"Your content (kept by both clean and --purge)"), @"items": @[
|
|
135
|
+
@[L(@"Pet sprites"), native[@"pets"]],
|
|
136
|
+
@[L(@"Shared lines"), native[@"phrases"]],
|
|
137
|
+
@[L(@"Per-pet lines"), native[@"petPhrases"]],
|
|
138
|
+
@[L(@"CC Bridge switch"), [CCBridgeHomeDirectory() stringByAppendingPathComponent:@"bridge-enabled"]],
|
|
139
|
+
@[@"codex / claude shim", CCPetsShimDirectory()]
|
|
140
|
+
]},
|
|
141
|
+
@{@"title": L(@"App data (removed by --purge; clean only clears the token cache and update log)"), @"items": @[
|
|
142
|
+
@[L(@"App data directory"), native[@"applicationSupport"]],
|
|
143
|
+
@[L(@"Quota history"), native[@"quotaHistory"]],
|
|
144
|
+
@[L(@"Auto-update config"), [native[@"applicationSupport"] stringByAppendingPathComponent:@"updater.json"]]
|
|
145
|
+
]},
|
|
146
|
+
@{@"title": L(@"Caches and runtime state (cleared by clean; safe to lose)"), @"items": @[
|
|
147
|
+
@[L(@"Pet store manifest cache"), native[@"petStoreCache"]],
|
|
148
|
+
@[L(@"Agent event stream"), native[@"agentEvents"]],
|
|
149
|
+
@[L(@"Claude quota snapshot"), native[@"claudeUsage"]],
|
|
150
|
+
@[L(@"Codex live quota"), native[@"codexLiveUsage"]],
|
|
151
|
+
@[L(@"Codex launch records"), native[@"codexLaunches"]],
|
|
152
|
+
@[L(@"Online client records"), native[@"clients"]],
|
|
153
|
+
@[L(@"CC Bridge state directory"), CCBridgeStateDirectory()]
|
|
154
|
+
]},
|
|
155
|
+
@{@"title": L(@"Program and integrations"), @"items": integrationItems}
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static int PrintPaths(BOOL asJSON) {
|
|
160
|
+
NSDictionary *native = NativePaths();
|
|
161
|
+
NSArray<NSDictionary *> *groups = PathGroups(native);
|
|
162
|
+
if (asJSON) {
|
|
163
|
+
NSMutableArray *documentGroups = [NSMutableArray array];
|
|
164
|
+
for (NSDictionary *group in groups) {
|
|
165
|
+
NSMutableArray *items = [NSMutableArray array];
|
|
166
|
+
for (NSArray *item in group[@"items"]) {
|
|
167
|
+
[items addObject:@{@"label": item[0], @"path": CCPetsTildePath(item[1]), @"bytes": SizeOf(item[1]) ?: NSNull.null}];
|
|
168
|
+
}
|
|
169
|
+
[documentGroups addObject:@{@"title": group[@"title"], @"items": items}];
|
|
170
|
+
}
|
|
171
|
+
NSDictionary *document = @{@"version": PackageVersion(), @"running": native[@"running"] ?: NSNull.null, @"groups": documentGroups};
|
|
172
|
+
NSData *json = [NSJSONSerialization dataWithJSONObject:document
|
|
173
|
+
options:NSJSONWritingPrettyPrinted | NSJSONWritingWithoutEscapingSlashes error:nil];
|
|
174
|
+
fwrite(json.bytes, 1, json.length, stdout);
|
|
175
|
+
fputc('\n', stdout);
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
NSUInteger width = 0;
|
|
179
|
+
for (NSDictionary *group in groups) {
|
|
180
|
+
for (NSArray *item in group[@"items"]) width = MAX(width, DisplayWidth(item[0]));
|
|
181
|
+
}
|
|
182
|
+
[groups enumerateObjectsUsingBlock:^(NSDictionary *group, NSUInteger index, BOOL *stop) {
|
|
183
|
+
if (index > 0) CCPetsPrintLine(stdout, @"");
|
|
184
|
+
CCPetsPrintLine(stdout, group[@"title"]);
|
|
185
|
+
for (NSArray *item in group[@"items"]) {
|
|
186
|
+
NSString *padding = [@"" stringByPaddingToLength:width - DisplayWidth(item[0]) + 2 withString:@" " startingAtIndex:0];
|
|
187
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:@" %@%@%@ (%@)", item[0], padding, CCPetsTildePath(item[1]),
|
|
188
|
+
FormatSize(SizeOf(item[1]))]);
|
|
189
|
+
}
|
|
190
|
+
}];
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#pragma mark - cc-pets doctor
|
|
195
|
+
|
|
196
|
+
static NSDictionary *Result(NSString *level, NSString *title, NSString *fix) {
|
|
197
|
+
return fix ? @{@"level": level, @"title": title, @"fix": fix} : @{@"level": level, @"title": title};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static NSString *VersionOfApp(NSString *appPath) {
|
|
201
|
+
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:[appPath stringByAppendingPathComponent:@"Contents/Info.plist"]];
|
|
202
|
+
NSString *version = [plist[@"CFBundleShortVersionString"] isKindOfClass:NSString.class] ? plist[@"CFBundleShortVersionString"] : nil;
|
|
203
|
+
return version.length > 0 ? version : nil;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 认出配置里属于 CC Pets 的 hook,并区分"指向当前安装"和"指向别处"(包被挪走、换了 node 版本重装后常见,
|
|
207
|
+
// hook 每次触发都会失败)。
|
|
208
|
+
static NSDictionary *InspectHooks(NSString *configFile, NSString *marker, NSString *legacyMarker) {
|
|
209
|
+
NSDictionary *read = ReadJSONFile(configFile);
|
|
210
|
+
if ([read[@"error"] isEqual:@"invalid"]) return @{@"error": @"invalid"};
|
|
211
|
+
NSDictionary *value = [read[@"value"] isKindOfClass:NSDictionary.class] ? read[@"value"] : nil;
|
|
212
|
+
NSDictionary *hooks = [value[@"hooks"] isKindOfClass:NSDictionary.class] ? value[@"hooks"] : nil;
|
|
213
|
+
NSMutableArray *commands = [NSMutableArray array];
|
|
214
|
+
for (id groups in hooks.allValues) {
|
|
215
|
+
if (![groups isKindOfClass:NSArray.class]) continue;
|
|
216
|
+
for (id group in groups) {
|
|
217
|
+
NSArray *handlers = [group isKindOfClass:NSDictionary.class] ? group[@"hooks"] : nil;
|
|
218
|
+
if (![handlers isKindOfClass:NSArray.class]) continue;
|
|
219
|
+
for (id handler in handlers) {
|
|
220
|
+
id command = [handler isKindOfClass:NSDictionary.class] ? handler[@"command"] : nil;
|
|
221
|
+
[commands addObject:command ?: NSNull.null];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
NSString *current = [NSString stringWithFormat:@"'%@'", PetBinary()];
|
|
226
|
+
NSUInteger total = 0, stale = 0, legacy = 0, bridge = 0;
|
|
227
|
+
for (id command in commands) {
|
|
228
|
+
if (CCPetsIsManagedHookCommand(command, marker, @"cc-pets")) {
|
|
229
|
+
total++;
|
|
230
|
+
if (![command containsString:current]) stale++;
|
|
231
|
+
}
|
|
232
|
+
if (CCPetsIsManagedHookCommand(command, legacyMarker, @"codex-pet")) legacy++;
|
|
233
|
+
if ([command isKindOfClass:NSString.class] && [command hasPrefix:CCBridgeHookMarker]) bridge++;
|
|
234
|
+
}
|
|
235
|
+
return @{@"total": @(total), @"stale": @(stale), @"legacy": @(legacy), @"bridge": @(bridge)};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static NSArray<NSDictionary *> *HookChecks(NSString *name, NSString *configFile, NSString *marker, NSString *legacyMarker,
|
|
239
|
+
NSUInteger *bridgeHooks) {
|
|
240
|
+
NSDictionary *hooks = InspectHooks(configFile, marker, legacyMarker);
|
|
241
|
+
*bridgeHooks = [hooks[@"bridge"] unsignedIntegerValue];
|
|
242
|
+
if (hooks[@"error"]) {
|
|
243
|
+
return @[Result(@"fail", LF(L(@"Can't parse {name} config: {configFile}"), (@{@"name": name, @"configFile": CCPetsTildePath(configFile)})),
|
|
244
|
+
L(@"Fix that JSON file, then run cc-pets install"))];
|
|
245
|
+
}
|
|
246
|
+
NSMutableArray<NSDictionary *> *results = [NSMutableArray array];
|
|
247
|
+
NSUInteger total = [hooks[@"total"] unsignedIntegerValue], stale = [hooks[@"stale"] unsignedIntegerValue];
|
|
248
|
+
NSUInteger legacy = [hooks[@"legacy"] unsignedIntegerValue];
|
|
249
|
+
if (total == 0) {
|
|
250
|
+
[results addObject:Result(@"fail", LF(L(@"{name} hooks not installed"), @{@"name": name}), @"cc-pets install")];
|
|
251
|
+
} else if (stale > 0) {
|
|
252
|
+
NSDictionary *values = @{@"name": name, @"stale": @(stale)};
|
|
253
|
+
[results addObject:Result(@"warn", stale != 1
|
|
254
|
+
? LF(L(@"{stale} {name} hooks point to cc-pets elsewhere (the package was moved or reinstalled)"), values)
|
|
255
|
+
: LF(L(@"{stale} {name} hook points to cc-pets elsewhere (the package was moved or reinstalled)"), values), @"cc-pets install")];
|
|
256
|
+
} else {
|
|
257
|
+
[results addObject:Result(@"ok", LF(L(@"{name} hooks installed ({total} events)"), (@{@"name": name, @"total": @(total)})), nil)];
|
|
258
|
+
}
|
|
259
|
+
if (legacy > 0) {
|
|
260
|
+
NSDictionary *values = @{@"name": name, @"legacy": @(legacy)};
|
|
261
|
+
[results addObject:Result(@"warn", legacy != 1
|
|
262
|
+
? LF(L(@"{name} config still has {legacy} old codex-pet hooks"), values)
|
|
263
|
+
: LF(L(@"{name} config still has {legacy} old codex-pet hook"), values), @"cc-pets install")];
|
|
264
|
+
}
|
|
265
|
+
return results;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static NSDictionary *StatusLineCheckAtHome(NSString *home) {
|
|
269
|
+
NSDictionary *value = ReadJSONFile([home stringByAppendingPathComponent:@"settings.json"])[@"value"];
|
|
270
|
+
NSDictionary *statusLine = [value isKindOfClass:NSDictionary.class] && [value[@"statusLine"] isKindOfClass:NSDictionary.class]
|
|
271
|
+
? value[@"statusLine"] : nil;
|
|
272
|
+
NSString *command = [statusLine[@"command"] isKindOfClass:NSString.class] ? statusLine[@"command"] : @"";
|
|
273
|
+
if (CCPetsIsManagedStatusLine(command, CCPetsStatusLineMarker)) {
|
|
274
|
+
return Result(@"ok", L(@"Claude status line feeds quota data through the wrapper"), nil);
|
|
275
|
+
}
|
|
276
|
+
NSString *script = CCPetsParseStatusLineCommand(command)[@"script"];
|
|
277
|
+
NSString *content = script ? ([NSString stringWithContentsOfFile:script encoding:NSUTF8StringEncoding error:nil] ?: @"") : @"";
|
|
278
|
+
if ([content containsString:CCPetsStatusLineStartMarker]) {
|
|
279
|
+
return [content containsString:[NSString stringWithFormat:@"'%@'", PetBinary()]]
|
|
280
|
+
? Result(@"ok", LF(L(@"Claude status line feeds quota data ({script})"), @{@"script": CCPetsTildePath(script)}), nil)
|
|
281
|
+
: Result(@"warn", L(@"The quota hook in the Claude status line points to cc-pets elsewhere"), @"cc-pets install");
|
|
282
|
+
}
|
|
283
|
+
return Result(@"warn", command.length > 0
|
|
284
|
+
? L(@"Claude status line doesn't feed quota data, so the pet can't show Claude quota %")
|
|
285
|
+
: L(@"No Claude status line configured, so the pet can't show Claude quota %"), @"cc-pets install");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
static NSArray<NSDictionary *> *ShimChecks(BOOL inApp) {
|
|
289
|
+
NSMutableArray<NSDictionary *> *results = [NSMutableArray array];
|
|
290
|
+
NSString *shellRC = ShellRC();
|
|
291
|
+
NSString *rc = [NSString stringWithContentsOfFile:shellRC encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
292
|
+
[results addObject:[rc containsString:CCPetsShimStartMarker]
|
|
293
|
+
? Result(@"ok", LF(L(@"{shellRC} adds the shim directory"), @{@"shellRC": CCPetsTildePath(shellRC)}), nil)
|
|
294
|
+
: Result(@"fail", LF(L(@"{shellRC} has no CC Pets shim setup"), @{@"shellRC": CCPetsTildePath(shellRC)}), @"cc-pets install")];
|
|
295
|
+
|
|
296
|
+
NSString *shimDirectory = CCPetsShimDirectory();
|
|
297
|
+
NSMutableArray<NSString *> *broken = [NSMutableArray array];
|
|
298
|
+
for (NSString *name in @[@"codex", @"claude"]) {
|
|
299
|
+
NSString *link = [NSFileManager.defaultManager destinationOfSymbolicLinkAtPath:[shimDirectory stringByAppendingPathComponent:name] error:nil];
|
|
300
|
+
if (!link) {
|
|
301
|
+
[broken addObject:LF(L(@"{name} (missing)"), @{@"name": name})];
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
NSString *target = [link hasPrefix:@"/"] ? link : [shimDirectory stringByAppendingPathComponent:link];
|
|
305
|
+
if (![NSFileManager.defaultManager fileExistsAtPath:CCPetsResolvePath(target)]) {
|
|
306
|
+
[broken addObject:LF(L(@"{name} (target doesn't exist)"), @{@"name": name})];
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
[results addObject:broken.count == 0
|
|
310
|
+
? Result(@"ok", LF(L(@"Shims complete: {shimDirectory}"), @{@"shimDirectory": CCPetsTildePath(shimDirectory)}), nil)
|
|
311
|
+
: Result(@"warn", LF(L(@"Shim problems: {broken}"), @{@"broken": [broken componentsJoinedByString:L(@", ")]}), @"cc-pets install")];
|
|
312
|
+
|
|
313
|
+
// 在桌宠进程里读到的是它自己的 PATH,和终端无关;判对错会误报,只给一条去终端确认的提示。
|
|
314
|
+
if (inApp) {
|
|
315
|
+
[results addObject:Result(@"info", L(@"The Main menu can't see your terminal's PATH; run cc-pets doctor in a terminal to check the shim order"), nil)];
|
|
316
|
+
return results;
|
|
317
|
+
}
|
|
318
|
+
NSMutableArray<NSString *> *entries = [NSMutableArray array];
|
|
319
|
+
for (NSString *entry in [CCPetsEnvironmentValue(@"PATH") ?: @"" componentsSeparatedByString:@":"]) {
|
|
320
|
+
if (entry.length > 0) [entries addObject:CCPetsResolvePath(entry)];
|
|
321
|
+
}
|
|
322
|
+
NSUInteger index = [entries indexOfObject:shimDirectory];
|
|
323
|
+
NSString *sourceHint = LF(L(@"source {shellRC}, or open a new terminal"), @{@"shellRC": CCPetsTildePath(shellRC)});
|
|
324
|
+
if (index == NSNotFound) {
|
|
325
|
+
[results addObject:Result(@"warn", L(@"This terminal's PATH lacks the shim directory, so typing codex / claude won't start the pet"), sourceHint)];
|
|
326
|
+
} else if (index > 0) {
|
|
327
|
+
NSString *shadowing = nil;
|
|
328
|
+
for (NSString *entry in [entries subarrayWithRange:NSMakeRange(0, index)]) {
|
|
329
|
+
for (NSString *name in @[@"codex", @"claude"]) {
|
|
330
|
+
if (!shadowing && [NSFileManager.defaultManager fileExistsAtPath:[entry stringByAppendingPathComponent:name]]) shadowing = entry;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
[results addObject:shadowing
|
|
334
|
+
? Result(@"warn", LF(L(@"{shadowing} comes before the shim directory in PATH and bypasses the pet"), @{@"shadowing": CCPetsTildePath(shadowing)}), sourceHint)
|
|
335
|
+
: Result(@"ok", L(@"The shim directory comes before the real codex / claude in PATH"), nil)];
|
|
336
|
+
} else {
|
|
337
|
+
[results addObject:Result(@"ok", L(@"The shim directory is first in PATH"), nil)];
|
|
338
|
+
}
|
|
339
|
+
return results;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// searchPath:主窗口传登录 shell 的 PATH,终端里传 nil 用自身 PATH。
|
|
343
|
+
static NSString *FindRealBinary(NSString *name, NSString *variable, NSString *searchPath) {
|
|
344
|
+
NSString *real = CCPetsFindRealExecutableInPath(name, variable, searchPath ?: CCPetsEnvironmentValue(@"PATH"));
|
|
345
|
+
return real && [NSFileManager.defaultManager fileExistsAtPath:real] ? real : nil;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static NSDictionary *RealBinaryCheck(NSString *name, NSString *variable, NSString *searchPath, BOOL inApp) {
|
|
349
|
+
NSString *real = FindRealBinary(name, variable, searchPath);
|
|
350
|
+
if (real) return Result(@"ok", LF(L(@"Found the real {name}: {real}"), (@{@"name": name, @"real": CCPetsTildePath(real)})), nil);
|
|
351
|
+
// 主窗口拿到的 PATH 未必等于终端里的(登录 shell 也可能起不来),找不到时不判错,交给终端里的 doctor。
|
|
352
|
+
if (inApp) {
|
|
353
|
+
return Result(@"info", LF(L(@"The Main menu couldn't find the real {name}; run cc-pets doctor in a terminal to confirm"), @{@"name": name}), nil);
|
|
354
|
+
}
|
|
355
|
+
return Result(@"warn", LF(L(@"The real {name} isn't in PATH, so the shim can't forward to it"), @{@"name": name}),
|
|
356
|
+
[NSString stringWithFormat:@"export %@=/absolute/path/to/%@", variable, name]);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static NSDictionary *UpdaterCheck(NSDictionary *native) {
|
|
360
|
+
NSString *file = [native[@"applicationSupport"] stringByAppendingPathComponent:@"updater.json"];
|
|
361
|
+
NSDictionary *read = ReadJSONFile(file);
|
|
362
|
+
NSString *fix = @"npm install -g cc-pets@latest --allow-scripts=cc-pets";
|
|
363
|
+
if (read[@"error"]) return Result(@"warn", L(@"Auto-update isn't configured, so in-app updates won't work"), fix);
|
|
364
|
+
NSDictionary *value = [read[@"value"] isKindOfClass:NSDictionary.class] ? read[@"value"] : @{};
|
|
365
|
+
NSMutableArray<NSString *> *missing = [NSMutableArray array];
|
|
366
|
+
for (NSString *key in @[@"nodePath", @"npmCliPath"]) {
|
|
367
|
+
NSString *path = [value[key] isKindOfClass:NSString.class] ? value[key] : nil;
|
|
368
|
+
if (path.length == 0 || ![NSFileManager.defaultManager fileExistsAtPath:path]) [missing addObject:key];
|
|
369
|
+
}
|
|
370
|
+
return missing.count == 0
|
|
371
|
+
? Result(@"ok", L(@"Auto-update config is valid"), nil)
|
|
372
|
+
: Result(@"warn", LF(L(@"The {missing} recorded for auto-update no longer exist (changed Node versions?)"),
|
|
373
|
+
@{@"missing": [missing componentsJoinedByString:L(@", ")]}), fix);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static NSArray<NSDictionary *> *DoctorSections(BOOL inApp);
|
|
377
|
+
|
|
378
|
+
NSArray<NSDictionary *> *CCPetsDoctorSections(void) {
|
|
379
|
+
return DoctorSections(NO);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
NSArray<NSDictionary *> *CCPetsDoctorSectionsForApp(void) {
|
|
383
|
+
return DoctorSections(YES);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
static NSArray<NSDictionary *> *DoctorSections(BOOL inApp) {
|
|
387
|
+
NSDictionary *native = NativePaths();
|
|
388
|
+
NSMutableArray<NSDictionary *> *sections = [NSMutableArray array];
|
|
389
|
+
BOOL claudeDetected = CCPetsClaudeCLIInstalled();
|
|
390
|
+
BOOL codexDetected = CCPetsCodexCLIInstalled();
|
|
391
|
+
NSString *packageVersion = PackageVersion();
|
|
392
|
+
|
|
393
|
+
NSMutableArray<NSDictionary *> *program = [NSMutableArray arrayWithObject:
|
|
394
|
+
Result(@"info", LF(L(@"npm package {packageVersion}: {projectDir}"),
|
|
395
|
+
(@{@"packageVersion": packageVersion, @"projectDir": CCPetsTildePath(PackageDirectory())})), nil)];
|
|
396
|
+
NSString *binaryVersion = @CC_PETS_VERSION;
|
|
397
|
+
if (![binaryVersion isEqualToString:packageVersion]) {
|
|
398
|
+
[program addObject:Result(@"warn", LF(L(@"Native binary {binaryVersion} doesn't match npm package {packageVersion}"),
|
|
399
|
+
(@{@"binaryVersion": binaryVersion, @"packageVersion": packageVersion})), @"cc-pets install")];
|
|
400
|
+
} else {
|
|
401
|
+
[program addObject:Result(@"ok", LF(L(@"Native binary {binaryVersion}"), @{@"binaryVersion": binaryVersion}), nil)];
|
|
402
|
+
}
|
|
403
|
+
NSString *appVersion = VersionOfApp(InstalledApp());
|
|
404
|
+
if (!appVersion) {
|
|
405
|
+
[program addObject:Result(@"warn", LF(L(@"Installed app not found: {installedApp}"), @{@"installedApp": CCPetsTildePath(InstalledApp())}), @"cc-pets install")];
|
|
406
|
+
} else if (![appVersion isEqualToString:packageVersion]) {
|
|
407
|
+
[program addObject:Result(@"warn", LF(L(@"Installed app {appVersion} doesn't match npm package {packageVersion}"),
|
|
408
|
+
(@{@"appVersion": appVersion, @"packageVersion": packageVersion})), @"cc-pets install")];
|
|
409
|
+
} else {
|
|
410
|
+
[program addObject:Result(@"ok", LF(L(@"Installed app {appVersion}"), @{@"appVersion": appVersion}), nil)];
|
|
411
|
+
}
|
|
412
|
+
[program addObject:Result(@"info", [native[@"running"] boolValue] ? L(@"The pet is running") : L(@"The pet isn't running"), nil)];
|
|
413
|
+
[program addObject:UpdaterCheck(native)];
|
|
414
|
+
[sections addObject:@{@"title": L(@"Program"), @"results": program}];
|
|
415
|
+
|
|
416
|
+
NSMutableDictionary<NSString *, NSNumber *> *profileBridgeHooks = [NSMutableDictionary dictionary];
|
|
417
|
+
NSArray<NSDictionary *> *registeredProfiles = CCPetsProfiles();
|
|
418
|
+
for (NSDictionary *profile in registeredProfiles) {
|
|
419
|
+
NSString *provider = profile[@"provider"];
|
|
420
|
+
NSString *name = profile[@"displayName"] ?: profile[@"slug"] ?: provider;
|
|
421
|
+
NSString *file = [profile[@"home"] stringByAppendingPathComponent:
|
|
422
|
+
[provider isEqualToString:CCPetsProfileProviderClaude] ? @"settings.json" : @"hooks.json"];
|
|
423
|
+
NSUInteger bridgeHooks = 0;
|
|
424
|
+
NSMutableArray *checks = [HookChecks(name, file,
|
|
425
|
+
[provider isEqualToString:CCPetsProfileProviderClaude] ? CCPetsClaudeHookMarker : CCPetsCodexHookMarker,
|
|
426
|
+
[provider isEqualToString:CCPetsProfileProviderClaude] ? CCPetsLegacyClaudeHookMarker : CCPetsLegacyCodexHookMarker,
|
|
427
|
+
&bridgeHooks) mutableCopy];
|
|
428
|
+
profileBridgeHooks[profile[@"id"]] = @(bridgeHooks);
|
|
429
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude]) {
|
|
430
|
+
[checks addObject:StatusLineCheckAtHome(profile[@"home"])];
|
|
431
|
+
} else {
|
|
432
|
+
[checks addObject:Result(@"info", L(@"Codex only runs CC Pets hooks after you trust them in /hooks (can't be checked automatically)"), nil)];
|
|
433
|
+
}
|
|
434
|
+
for (NSUInteger index = 0; index < checks.count; index++) {
|
|
435
|
+
NSDictionary *check = checks[index];
|
|
436
|
+
if ([check[@"fix"] isEqualToString:@"cc-pets install"]) {
|
|
437
|
+
NSMutableDictionary *repairable = [check mutableCopy];
|
|
438
|
+
repairable[@"repairProfileID"] = profile[@"id"];
|
|
439
|
+
checks[index] = repairable;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
[sections addObject:@{@"title": name, @"profileID": profile[@"id"], @"results": checks}];
|
|
443
|
+
}
|
|
444
|
+
if (registeredProfiles.count == 0) {
|
|
445
|
+
[sections addObject:@{@"title": L(@"Agent profiles"), @"results": @[
|
|
446
|
+
Result(@"info", L(@"No registered Agent profiles"), nil)]}];
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Finder 拉起的主窗口只有 launchd 的最小 PATH,nvm / Homebrew 下的 CLI 都不在里面,改用登录 shell 的。
|
|
450
|
+
NSString *searchPath = inApp && (codexDetected || claudeDetected) ? CCPetsLoginShellPath() : nil;
|
|
451
|
+
NSMutableArray *shell = [ShimChecks(inApp) mutableCopy];
|
|
452
|
+
if (codexDetected) [shell addObject:RealBinaryCheck(@"codex", @"CODEX_REAL_BIN", searchPath, inApp)];
|
|
453
|
+
if (claudeDetected) [shell addObject:RealBinaryCheck(@"claude", @"CLAUDE_REAL_BIN", searchPath, inApp)];
|
|
454
|
+
[sections addObject:@{@"title": L(@"Shell integration"), @"results": shell}];
|
|
455
|
+
|
|
456
|
+
if (!CCBridgeEnabled()) {
|
|
457
|
+
[sections addObject:@{@"title": @"CC Bridge", @"results": @[Result(@"info", L(@"Off (optional; turn on with cc-pets bridge enable)"), nil)]}];
|
|
458
|
+
} else {
|
|
459
|
+
NSMutableArray *bridge = [NSMutableArray arrayWithObject:Result(@"ok", L(@"Enabled"), nil)];
|
|
460
|
+
for (NSDictionary *profile in registeredProfiles) {
|
|
461
|
+
NSString *name = profile[@"displayName"] ?: profile[@"slug"] ?: profile[@"provider"];
|
|
462
|
+
NSDictionary *result = [profileBridgeHooks[profile[@"id"]] unsignedIntegerValue] > 0
|
|
463
|
+
? Result(@"ok", [NSString stringWithFormat:L(@"CC Bridge hooks installed in %@"), name], nil)
|
|
464
|
+
: Result(@"warn", [NSString stringWithFormat:L(@"CC Bridge hooks missing in %@"), name], @"cc-pets bridge enable");
|
|
465
|
+
NSMutableDictionary *check = [result mutableCopy];
|
|
466
|
+
check[@"profileID"] = profile[@"id"];
|
|
467
|
+
[bridge addObject:check];
|
|
468
|
+
}
|
|
469
|
+
if (codexDetected) {
|
|
470
|
+
if ([CCBridgeOptions()[@"wake"] boolValue]) {
|
|
471
|
+
// npm 装的 codex 是 `env node` 脚本,探测时也得带上能找到 node 的 PATH。
|
|
472
|
+
NSMutableDictionary<NSString *, NSString *> *environment = nil;
|
|
473
|
+
if (searchPath) {
|
|
474
|
+
environment = [NSProcessInfo.processInfo.environment mutableCopy];
|
|
475
|
+
environment[@"PATH"] = searchPath;
|
|
476
|
+
}
|
|
477
|
+
NSDictionary *probe = CCBridgeProbeCodexQueueWithEnvironment(FindRealBinary(@"codex", @"CODEX_REAL_BIN", searchPath), environment);
|
|
478
|
+
NSString *notice = CCBridgeDescribeCodexQueue(probe);
|
|
479
|
+
if (inApp && [probe[@"status"] isEqualToString:@"missing"]) {
|
|
480
|
+
[bridge addObject:Result(@"info", L(@"The Main menu couldn't find codex to check whether it can be woken; run cc-pets doctor in a terminal to confirm"), nil)];
|
|
481
|
+
} else {
|
|
482
|
+
[bridge addObject:notice ? Result(@"warn", notice, nil)
|
|
483
|
+
: Result(@"ok", L(@"Codex supports codex queue, so it can be woken automatically"), nil)];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
[sections addObject:@{@"title": @"CC Bridge", @"results": bridge}];
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return sections;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
static NSUInteger CountResults(NSArray<NSDictionary *> *sections, NSString *level) {
|
|
494
|
+
NSUInteger count = 0;
|
|
495
|
+
for (NSDictionary *section in sections) {
|
|
496
|
+
for (NSDictionary *result in section[@"results"]) {
|
|
497
|
+
if ([result[@"level"] isEqualToString:level]) count++;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return count;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
BOOL CCPetsDoctorHasFailures(NSArray<NSDictionary *> *sections) {
|
|
504
|
+
return CountResults(sections, @"fail") > 0;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
NSString *CCPetsDoctorReport(NSArray<NSDictionary *> *sections) {
|
|
508
|
+
struct utsname system;
|
|
509
|
+
uname(&system);
|
|
510
|
+
NSMutableArray<NSString *> *lines = [NSMutableArray arrayWithObject:
|
|
511
|
+
LF(L(@"CC Pets diagnostics (npm package {packageVersion}, {platform} {release})"),
|
|
512
|
+
(@{@"packageVersion": PackageVersion(), @"platform": @"darwin", @"release": @(system.release)}))];
|
|
513
|
+
NSDictionary<NSString *, NSString *> *icons = @{@"ok": @"✅", @"warn": @"⚠️ ", @"fail": @"❌", @"info": @"ℹ️ "};
|
|
514
|
+
for (NSDictionary *section in sections) {
|
|
515
|
+
[lines addObject:[@"\n" stringByAppendingString:section[@"title"]]];
|
|
516
|
+
for (NSDictionary *result in section[@"results"]) {
|
|
517
|
+
[lines addObject:[NSString stringWithFormat:@" %@ %@", icons[result[@"level"]], result[@"title"]]];
|
|
518
|
+
if (result[@"fix"]) [lines addObject:[NSString stringWithFormat:@" → %@", result[@"fix"]]];
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
NSUInteger warn = CountResults(sections, @"warn"), fail = CountResults(sections, @"fail");
|
|
522
|
+
NSString *warnings = warn != 1 ? LF(L(@"{count} warnings"), @{@"count": @(warn)}) : LF(L(@"{count} warning"), @{@"count": @(warn)});
|
|
523
|
+
NSString *errors = fail != 1 ? LF(L(@"{count} errors"), @{@"count": @(fail)}) : LF(L(@"{count} error"), @{@"count": @(fail)});
|
|
524
|
+
[lines addObject:LF(L(@"\nResult: {ok} OK, {warnings}, {errors}."),
|
|
525
|
+
(@{@"ok": @(CountResults(sections, @"ok")), @"warnings": warnings, @"errors": errors}))];
|
|
526
|
+
if (fail + warn > 0) [lines addObject:L(@"Most problems are fixed by cc-pets install; see cc-pets paths for data locations.")];
|
|
527
|
+
return [lines componentsJoinedByString:@"\n"];
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
static int RunDoctor(void) {
|
|
531
|
+
NSArray<NSDictionary *> *sections = CCPetsDoctorSections();
|
|
532
|
+
CCPetsPrintLine(stdout, CCPetsDoctorReport(sections));
|
|
533
|
+
return CCPetsDoctorHasFailures(sections) ? 1 : 0;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// 不运行完整安装脚本:它会安装 App、改 shell 并重启当前进程。
|
|
537
|
+
// 诊断页只修复明确登记过的账号的 Hooks / status line,沿用安装器保留第三方配置的逻辑。
|
|
538
|
+
static int RepairProfile(NSString *profileID) {
|
|
539
|
+
NSDictionary *profile = CCPetsProfileWithID(profileID);
|
|
540
|
+
if (!profile || !CCPetsDirectoryExists(profile[@"home"])) {
|
|
541
|
+
CCPetsPrintLine(stderr, L(@"The Agent profile or its Home is no longer available."));
|
|
542
|
+
return 1;
|
|
543
|
+
}
|
|
544
|
+
NSString *binary = PetBinary();
|
|
545
|
+
if (![NSFileManager.defaultManager isExecutableFileAtPath:binary]) {
|
|
546
|
+
CCPetsPrintLine(stderr, L(@"The CC Pets package binary is missing. Run cc-pets install in a terminal."));
|
|
547
|
+
return 1;
|
|
548
|
+
}
|
|
549
|
+
BOOL claude = [profile[@"provider"] isEqualToString:CCPetsProfileProviderClaude];
|
|
550
|
+
NSArray<NSString *> *files = claude ? CCPetsClaudeIntegrationFilesAtHome(profile[@"home"])
|
|
551
|
+
: @[[profile[@"home"] stringByAppendingPathComponent:@"hooks.json"]];
|
|
552
|
+
NSString *parseError = nil;
|
|
553
|
+
id config = CCPetsReadConfig(files.firstObject, &parseError);
|
|
554
|
+
if (parseError || (config && ![config isKindOfClass:CCPetsJSONObject.class])) {
|
|
555
|
+
CCPetsPrintLine(stderr, parseError ?: L(@"The Agent configuration must be a JSON object."));
|
|
556
|
+
return 1;
|
|
557
|
+
}
|
|
558
|
+
NSString *backup = [[ApplicationSupportDirectory() stringByAppendingPathComponent:@"diagnostic-backups"]
|
|
559
|
+
stringByAppendingPathComponent:NSUUID.UUID.UUIDString];
|
|
560
|
+
NSError *error = nil;
|
|
561
|
+
if (![NSFileManager.defaultManager createDirectoryAtPath:backup withIntermediateDirectories:YES
|
|
562
|
+
attributes:@{NSFilePosixPermissions: @0700} error:&error]) {
|
|
563
|
+
CCPetsPrintLine(stderr, error.localizedDescription);
|
|
564
|
+
return 1;
|
|
565
|
+
}
|
|
566
|
+
NSMutableArray<NSDictionary *> *manifest = [NSMutableArray array];
|
|
567
|
+
for (NSString *file in files) {
|
|
568
|
+
struct stat info;
|
|
569
|
+
BOOL exists = lstat(file.fileSystemRepresentation, &info) == 0;
|
|
570
|
+
if (!exists && errno != ENOENT) {
|
|
571
|
+
CCPetsPrintLine(stderr, [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil].localizedDescription);
|
|
572
|
+
return 1;
|
|
573
|
+
}
|
|
574
|
+
NSMutableDictionary *entry = [@{@"path": file, @"existed": @(exists)} mutableCopy];
|
|
575
|
+
if (exists) {
|
|
576
|
+
NSString *name = [NSString stringWithFormat:@"%lu-%@", (unsigned long)manifest.count, file.lastPathComponent];
|
|
577
|
+
NSData *data = [NSData dataWithContentsOfFile:file options:0 error:&error];
|
|
578
|
+
if (!data || ![NSFileManager.defaultManager createFileAtPath:[backup stringByAppendingPathComponent:name]
|
|
579
|
+
contents:data attributes:@{NSFilePosixPermissions: @0600}]) {
|
|
580
|
+
CCPetsPrintLine(stderr, error.localizedDescription ?: L(@"Could not back up the configuration. No repair was attempted."));
|
|
581
|
+
return 1;
|
|
582
|
+
}
|
|
583
|
+
entry[@"backup"] = name;
|
|
584
|
+
entry[@"mode"] = @(info.st_mode & 07777);
|
|
585
|
+
}
|
|
586
|
+
[manifest addObject:entry];
|
|
587
|
+
}
|
|
588
|
+
NSData *json = [NSJSONSerialization dataWithJSONObject:manifest options:NSJSONWritingPrettyPrinted error:&error];
|
|
589
|
+
if (!json || ![NSFileManager.defaultManager createFileAtPath:[backup stringByAppendingPathComponent:@"manifest.json"]
|
|
590
|
+
contents:json attributes:@{NSFilePosixPermissions: @0600}]) {
|
|
591
|
+
CCPetsPrintLine(stderr, error.localizedDescription ?: L(@"Could not back up the configuration. No repair was attempted."));
|
|
592
|
+
return 1;
|
|
593
|
+
}
|
|
594
|
+
CCPetsPrintLine(stdout, [NSString stringWithFormat:L(@"Configuration backup: %@"), CCPetsTildePath(backup)]);
|
|
595
|
+
int status = claude ? InstallClaudeHooksAtHome(binary, profile[@"home"])
|
|
596
|
+
: InstallCodexHooksAtHome(binary, profile[@"home"]);
|
|
597
|
+
if (status != 0) return status;
|
|
598
|
+
|
|
599
|
+
// 安装返回成功后重新读配置;未恢复的 Hooks / status line 不能清掉待修复状态。
|
|
600
|
+
NSUInteger bridgeHooks = 0;
|
|
601
|
+
NSMutableArray<NSDictionary *> *checks = [HookChecks(profile[@"displayName"] ?: profileID,
|
|
602
|
+
files.firstObject, claude ? CCPetsClaudeHookMarker : CCPetsCodexHookMarker,
|
|
603
|
+
claude ? CCPetsLegacyClaudeHookMarker : CCPetsLegacyCodexHookMarker, &bridgeHooks) mutableCopy];
|
|
604
|
+
if (claude) [checks addObject:StatusLineCheckAtHome(profile[@"home"])];
|
|
605
|
+
for (NSDictionary *check in checks) {
|
|
606
|
+
if ([check[@"level"] isEqualToString:@"ok"]) continue;
|
|
607
|
+
CCPetsPrintLine(stderr, check[@"title"]);
|
|
608
|
+
return 1;
|
|
609
|
+
}
|
|
610
|
+
NSMutableDictionary *verified = [CCPetsProfileWithID(profileID) mutableCopy];
|
|
611
|
+
[verified removeObjectForKey:@"needsRepair"];
|
|
612
|
+
if (verified && [CCPetsProfileStatus(verified)[@"state"] isEqualToString:CCPetsProfileStateIntegrated]) {
|
|
613
|
+
NSString *message = nil;
|
|
614
|
+
if (!CCPetsSetProfileNeedsRepair(profileID, NO, &message)) {
|
|
615
|
+
CCPetsPrintLine(stderr, message);
|
|
616
|
+
return 1;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return 0;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
int CCPetsDoctorMain(NSArray<NSString *> *arguments) {
|
|
623
|
+
NSString *command = arguments.firstObject;
|
|
624
|
+
if ([command isEqualToString:@"paths"]) return PrintPaths([arguments containsObject:@"--json"]);
|
|
625
|
+
if ([command isEqualToString:@"doctor"]) {
|
|
626
|
+
if (arguments.count == 3 && [arguments[1] isEqualToString:@"--repair-profile"]) return RepairProfile(arguments[2]);
|
|
627
|
+
if (arguments.count != 1) return 2;
|
|
628
|
+
return RunDoctor();
|
|
629
|
+
}
|
|
630
|
+
CCPetsPrintLine(stderr, L(@"Usage: cc-pets paths [--json] | cc-pets doctor"));
|
|
631
|
+
return 2;
|
|
632
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 保序 JSON,用于改写用户自己的配置文件(~/.claude/settings.json、~/.codex/hooks.json)。
|
|
4
|
+
// NSJSONSerialization 不保留键顺序,输出格式也和 JSON.stringify 不同("key" : value、转义 /),
|
|
5
|
+
// 拿它回写会把用户的文件整个打乱。这里的约定与原先 Node 安装器的 JSON.parse + JSON.stringify(…, null, 2)
|
|
6
|
+
// 一致:对象按键的首次出现顺序输出,重复键后者覆盖前者的值但不改变位置。
|
|
7
|
+
//
|
|
8
|
+
// 值的表示:对象 CCPetsJSONObject、数组 NSMutableArray、字符串 NSString、null NSNull、
|
|
9
|
+
// 布尔 @YES/@NO、解析出的数字 CCPetsJSONNumber(保留原文字面量,不经过 double 往返)。
|
|
10
|
+
// 代码里新建的数字可以直接用非布尔的 NSNumber;普通 NSDictionary 也能写出,键按字母序排列。
|
|
11
|
+
|
|
12
|
+
@interface CCPetsJSONObject : NSObject <NSCopying>
|
|
13
|
+
@property (nonatomic, readonly) NSArray<NSString *> *keys;
|
|
14
|
+
- (id)objectForKeyedSubscript:(NSString *)key;
|
|
15
|
+
// 已有的键原位替换值,新键追加到末尾,与 JS 对象的属性顺序规则相同。
|
|
16
|
+
- (void)setObject:(id)value forKeyedSubscript:(NSString *)key;
|
|
17
|
+
- (void)removeObjectForKey:(NSString *)key;
|
|
18
|
+
@end
|
|
19
|
+
|
|
20
|
+
@interface CCPetsJSONNumber : NSObject
|
|
21
|
+
@property (nonatomic, readonly, copy) NSString *literal;
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
// 解析失败返回 nil,并通过 errorMessage 给出出错位置。
|
|
25
|
+
id CCPetsJSONParse(NSData *data, NSString **errorMessage);
|
|
26
|
+
// 等价于 JSON.stringify(value, null, 2)。
|
|
27
|
+
NSString *CCPetsJSONStringify(id value);
|