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
|
@@ -6,10 +6,8 @@ NSString *const PetSpeechFrequencyKey = @"CCPetsSpeechFrequency";
|
|
|
6
6
|
|
|
7
7
|
@implementation AppDelegate (Speech)
|
|
8
8
|
- (BOOL)speechEnabled {
|
|
9
|
-
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
10
9
|
// 缺省开启;用户可在右键菜单里关掉。
|
|
11
|
-
|
|
12
|
-
return value == nil ? YES : [value boolValue];
|
|
10
|
+
return CCPetsPrefBool(PetSpeechEnabledKey);
|
|
13
11
|
}
|
|
14
12
|
// 当前档位。现读,右键菜单里改完立刻生效不用重启。
|
|
15
13
|
- (PetSpeechRate)speechRate {
|
|
@@ -147,6 +145,8 @@ NSString *const PetSpeechFrequencyKey = @"CCPetsSpeechFrequency";
|
|
|
147
145
|
self.speechPanel.level = NSFloatingWindowLevel;
|
|
148
146
|
self.speechPanel.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces |
|
|
149
147
|
NSWindowCollectionBehaviorFullScreenAuxiliary;
|
|
148
|
+
// 和桌宠、状态卡一样不随 App 失焦隐藏。主窗口打开期间 App 是 Regular,会频繁失焦。
|
|
149
|
+
self.speechPanel.hidesOnDeactivate = NO;
|
|
150
150
|
// 气泡不抢焦点、不接受点击:它是纯播报,挡住宠物就本末倒置了。
|
|
151
151
|
self.speechPanel.ignoresMouseEvents = YES;
|
|
152
152
|
|
|
@@ -210,6 +210,10 @@ NSString *const PetSpeechFrequencyKey = @"CCPetsSpeechFrequency";
|
|
|
210
210
|
|
|
211
211
|
[self presentSpeechText:text];
|
|
212
212
|
}
|
|
213
|
+
// 设置页"试说一句"和预览台用:只取词,不记预算、不进冷却,说不说由调用方决定。
|
|
214
|
+
- (NSString *)sampleSpeechLine {
|
|
215
|
+
return PetPhraseForTag(PetPhraseTagIdle, [self speechSlots]);
|
|
216
|
+
}
|
|
213
217
|
// 话往哪儿显示只在这一处决定:有状态卡就并进它的副行,没有才起独立气泡。
|
|
214
218
|
// 调试触发也必须走这里——否则看到的是现实中不会出现的画面(两个泡叠着)。
|
|
215
219
|
- (void)presentSpeechText:(NSString *)text {
|
|
@@ -389,6 +393,6 @@ NSString *const PetSpeechFrequencyKey = @"CCPetsSpeechFrequency";
|
|
|
389
393
|
self.speechGlass.frame = NSMakeRect(6, 6, width, PetSpeechBodyHeight);
|
|
390
394
|
self.speechGlass.cornerRadius = PetSpeechBodyHeight / 2.0;
|
|
391
395
|
self.speechLabel.frame = NSMakeRect(padding, 10, width - padding * 2, 18);
|
|
392
|
-
self.speechClickButton.frame =
|
|
396
|
+
self.speechClickButton.frame = NSMakeRect(0, 0, panelSize.width, panelSize.height);
|
|
393
397
|
}
|
|
394
398
|
@end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// 主窗口工作台几页的数据快照(见 CCPetsWorkbench.h)。只读:整理现有的会话、Bridge、额度状态,
|
|
2
|
+
// 不新增任何采集。
|
|
3
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
4
|
+
#import "CCPetsBridge.h"
|
|
5
|
+
#import "CCPetsDiagnosticsPage.h"
|
|
6
|
+
#import "CCPetsUsageMode.h"
|
|
7
|
+
|
|
8
|
+
NSNotificationName const CCPetsWorkbenchQuotasDidChangeNotification = @"CCPetsWorkbenchQuotasDidChange";
|
|
9
|
+
|
|
10
|
+
@implementation AppDelegate (Workbench)
|
|
11
|
+
|
|
12
|
+
- (NSArray<NSDictionary *> *)workbenchSessions {
|
|
13
|
+
[self pruneOfflineAgentSessionRecords];
|
|
14
|
+
NSDictionary<NSString *, NSNumber *> *pending = self.bridgePendingCounts ?: @{};
|
|
15
|
+
NSMutableArray<NSDictionary *> *sessions = [NSMutableArray array];
|
|
16
|
+
for (NSString *key in self.agentSessionRecords) {
|
|
17
|
+
NSDictionary *record = self.agentSessionRecords[key];
|
|
18
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32).lowercaseString;
|
|
19
|
+
// 和状态气泡一致:启动后一直没有后续事件,说明会话已就绪、在等输入。
|
|
20
|
+
NSString *state = SessionDisplayState([record[@"state"] isKindOfClass:NSString.class]
|
|
21
|
+
? record[@"state"] : @"", [record[@"timestamp"] doubleValue], NSDate.date.timeIntervalSince1970);
|
|
22
|
+
NSString *tool = [record[@"tool"] isKindOfClass:NSString.class] ? record[@"tool"] : @"";
|
|
23
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
|
|
24
|
+
NSMutableDictionary *session = [NSMutableDictionary dictionary];
|
|
25
|
+
session[@"id"] = key;
|
|
26
|
+
session[@"provider"] = [provider isEqualToString:@"claude"] ? @"Claude"
|
|
27
|
+
: ([provider isEqualToString:@"codex"] ? @"Codex" : (provider.length > 0 ? provider : @"Agent"));
|
|
28
|
+
if ([record[@"profileId"] isKindOfClass:NSString.class]) session[@"profileId"] = record[@"profileId"];
|
|
29
|
+
if ([record[@"profileName"] isKindOfClass:NSString.class]) session[@"profileName"] = record[@"profileName"];
|
|
30
|
+
session[@"state"] = state;
|
|
31
|
+
session[@"status"] = [self statusTextForState:state tool:tool];
|
|
32
|
+
session[@"tool"] = SanitizedShortString(tool, 64) ?: @"";
|
|
33
|
+
session[@"terminal"] = terminal.count > 0 ? [self terminalNameForTarget:terminal] : @"";
|
|
34
|
+
session[@"updatedAt"] = record[@"timestamp"] ?: @0;
|
|
35
|
+
NSDictionary *bridge = [self bridgeSessionEntryForRecord:record];
|
|
36
|
+
if ([bridge[@"name"] isKindOfClass:NSString.class]) session[@"name"] = bridge[@"name"];
|
|
37
|
+
NSString *bridgeID = [self bridgeSessionIdForRecord:record];
|
|
38
|
+
session[@"pendingMessages"] = bridgeID ? (pending[bridgeID] ?: @0) : @0;
|
|
39
|
+
[sessions addObject:session];
|
|
40
|
+
}
|
|
41
|
+
// 和右键会话菜单同一个次序:等审批的置顶,其余新的在前。
|
|
42
|
+
[sessions sortUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
43
|
+
BOOL leftApproval = [left[@"state"] isEqualToString:@"approval"];
|
|
44
|
+
BOOL rightApproval = [right[@"state"] isEqualToString:@"approval"];
|
|
45
|
+
if (leftApproval != rightApproval) return leftApproval ? NSOrderedAscending : NSOrderedDescending;
|
|
46
|
+
return [right[@"updatedAt"] compare:left[@"updatedAt"]];
|
|
47
|
+
}];
|
|
48
|
+
return sessions;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
- (NSDictionary *)workbenchMessages {
|
|
52
|
+
if (!CCBridgeEnabled()) return @{@"enabled": @NO, @"pending": @[], @"deliveries": @[]};
|
|
53
|
+
[self refreshBridgeState:nil];
|
|
54
|
+
NSMutableArray<NSDictionary *> *pending = [NSMutableArray array];
|
|
55
|
+
[self.bridgePendingCounts enumerateKeysAndObjectsUsingBlock:^(NSString *session, NSNumber *count, BOOL *stop) {
|
|
56
|
+
NSString *name = self.bridgeSessions[session][@"name"];
|
|
57
|
+
[pending addObject:@{@"session": session, @"name": [name isKindOfClass:NSString.class] ? name : session,
|
|
58
|
+
@"count": count}];
|
|
59
|
+
}];
|
|
60
|
+
[pending sortUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
61
|
+
return [left[@"name"] localizedStandardCompare:right[@"name"]];
|
|
62
|
+
}];
|
|
63
|
+
NSMutableArray<NSDictionary *> *deliveries = [NSMutableArray array];
|
|
64
|
+
for (NSDictionary *delivery in self.bridgeRecentDeliveries) {
|
|
65
|
+
NSMutableDictionary *entry = [delivery mutableCopy];
|
|
66
|
+
entry[@"unseen"] = @([delivery[@"at"] doubleValue] > self.bridgeSeenAt);
|
|
67
|
+
[deliveries addObject:entry];
|
|
68
|
+
}
|
|
69
|
+
return @{@"enabled": @YES, @"pending": pending, @"deliveries": deliveries};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
- (NSArray<NSDictionary *> *)workbenchQuotas {
|
|
73
|
+
NSMutableArray<NSDictionary *> *quotas = [NSMutableArray array];
|
|
74
|
+
// 平台次序和悬停面板一致,但概览同时列出全部账号,不跟随轮播选择。
|
|
75
|
+
for (NSString *provider in [self.quotaView visibleProviders]) {
|
|
76
|
+
NSMutableArray<NSDictionary *> *profiles = [NSMutableArray array];
|
|
77
|
+
for (NSDictionary *profile in self.quotaView.profiles ?: CCPetsProfiles()) {
|
|
78
|
+
if ([profile[@"provider"] isEqualToString:provider.lowercaseString]) [profiles addObject:profile];
|
|
79
|
+
}
|
|
80
|
+
// 未登记账号时保留旧版的平台卡片。
|
|
81
|
+
if (profiles.count == 0) [profiles addObject:@{}];
|
|
82
|
+
for (NSDictionary *profile in profiles) {
|
|
83
|
+
NSString *profileID = profile[@"id"];
|
|
84
|
+
NSMutableDictionary *quota = [[self.quotaView accountSnapshotForProvider:provider
|
|
85
|
+
profile:profile.count > 0 ? profile : nil] mutableCopy];
|
|
86
|
+
NSDictionary *usage = quota[@"usage"];
|
|
87
|
+
NSString *usedKey = [provider isEqualToString:@"Claude"] ? @"used_percentage" : @"used_percent";
|
|
88
|
+
NSNumber *weekUsed = nil;
|
|
89
|
+
for (NSArray<NSString *> *window in @[@[@"fiveHour", @"five"], @[@"week", @"week"]]) {
|
|
90
|
+
NSDictionary *limit = [usage[window[0]] isKindOfClass:NSDictionary.class] ? usage[window[0]] : nil;
|
|
91
|
+
NSNumber *used = [limit[usedKey] isKindOfClass:NSNumber.class] ? limit[usedKey] : nil;
|
|
92
|
+
if (used) {
|
|
93
|
+
double clamped = fmax(0, fmin(100, used.doubleValue));
|
|
94
|
+
quota[[window[1] stringByAppendingString:@"Remaining"]] = @(100 - clamped);
|
|
95
|
+
if ([window[1] isEqualToString:@"week"]) weekUsed = @(clamped);
|
|
96
|
+
}
|
|
97
|
+
if ([limit[@"resets_at"] isKindOfClass:NSNumber.class]) {
|
|
98
|
+
quota[[window[1] stringByAppendingString:@"Reset"]] = limit[@"resets_at"];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (![quota[@"apiMode"] boolValue]) {
|
|
102
|
+
NSDictionary *week = [usage[@"week"] isKindOfClass:NSDictionary.class] ? usage[@"week"] : nil;
|
|
103
|
+
NSDictionary *pace = [self.quotaView paceStatusForQuota:week currentUsed:weekUsed color:NSColor.systemTealColor];
|
|
104
|
+
if (pace[@"level"]) {
|
|
105
|
+
quota[@"pace"] = pace[@"label"];
|
|
106
|
+
quota[@"paceLevel"] = pace[@"level"];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (NSDictionary *section in CCPetsDiagnosticsStore.sections) {
|
|
110
|
+
if (!profileID) continue;
|
|
111
|
+
for (NSDictionary *result in section[@"results"]) {
|
|
112
|
+
if (![section[@"profileID"] isEqualToString:profileID] &&
|
|
113
|
+
![result[@"profileID"] isEqualToString:profileID]) continue;
|
|
114
|
+
if ([@[@"warn", @"fail"] containsObject:result[@"level"]]) quota[@"hasDiagnosticIssues"] = @YES;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
[quotas addObject:quota];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return quotas;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
- (void)focusWorkbenchSessionWithID:(NSString *)sessionID {
|
|
124
|
+
NSDictionary *record = sessionID ? self.agentSessionRecords[sessionID] : nil;
|
|
125
|
+
NSDictionary *target = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
|
|
126
|
+
if (target.count > 0) ActivateTerminalFocusTarget(target);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
- (void)focusBridgeSessionWithID:(NSString *)sessionID {
|
|
130
|
+
NSDictionary *target = sessionID ? [self terminalTargetForBridgeSession:sessionID] : nil;
|
|
131
|
+
if (target.count > 0) ActivateTerminalFocusTarget(target);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
- (void)markWorkbenchMessagesSeen {
|
|
135
|
+
self.bridgeSeenAt = NSDate.date.timeIntervalSince1970;
|
|
136
|
+
[self refreshBridgeState:nil];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@end
|
|
@@ -51,6 +51,12 @@
|
|
|
51
51
|
@property NSDictionary<NSString *, NSNumber *> *bridgePendingCounts;
|
|
52
52
|
// 菜单开关触发的 cc-pets bridge 命令正在执行时为 YES,期间忽略新的切换,避免并发改配置。
|
|
53
53
|
@property BOOL bridgeCommandRunning;
|
|
54
|
+
// 手动 Agent Home 适配任务与其非阻塞进度窗口。
|
|
55
|
+
@property BOOL profileCommandRunning;
|
|
56
|
+
@property NSTask *profileTask;
|
|
57
|
+
@property NSWindow *profileProgressWindow;
|
|
58
|
+
@property NSTextField *profileProgressLabel;
|
|
59
|
+
@property NSProgressIndicator *profileProgressIndicator;
|
|
54
60
|
// 已为哪个时间点之前的送达发过系统通知。
|
|
55
61
|
@property NSTimeInterval bridgeNotifiedAt;
|
|
56
62
|
@property BOOL statusBubbleExpanded;
|
|
@@ -59,9 +65,16 @@
|
|
|
59
65
|
@property NSInteger liveClientCount;
|
|
60
66
|
@property NSString *lastStatusState;
|
|
61
67
|
@property NSString *lastStatusProvider;
|
|
68
|
+
// lastStatusProvider 是卡片标题(有账号名时显示账号名),不能拿去对 pid 文件里的
|
|
69
|
+
// "Claude"/"Codex";在线判定用原始 provider,判断“是不是同一账号”用 provider|profileId。
|
|
70
|
+
@property NSString *lastStatusAgentProvider;
|
|
71
|
+
@property NSString *lastStatusIdentity;
|
|
62
72
|
@property NSDictionary *lastTerminalFocusTarget;
|
|
63
73
|
@property NSTimeInterval lastStatusTimestamp;
|
|
64
74
|
@property NSSet<NSString *> *liveClientProviders;
|
|
75
|
+
// 新版包装器在 pid 文件中同时记录 profileId;旧文件没有该行,按 provider 级在线兜底。
|
|
76
|
+
@property NSSet<NSString *> *liveClientProfileIDs;
|
|
77
|
+
@property NSSet<NSString *> *liveUnscopedClientProviders;
|
|
65
78
|
// 包装脚本仍存活的精确 Agent 会话,键为 provider + TTY。
|
|
66
79
|
@property NSSet<NSString *> *liveAgentSessionKeys;
|
|
67
80
|
// 每个 provider 最近一次事件的时间。不经包装脚本启动的客户端没有 pid 文件,
|
|
@@ -76,6 +89,8 @@
|
|
|
76
89
|
@property NSMutableSet<NSString *> *stallNotifiedSessionKeys;
|
|
77
90
|
@property QuotaDashboardView *quotaView;
|
|
78
91
|
@property BOOL managedByCLI;
|
|
92
|
+
// 启动参数 --open-window [page] 带来的:启动完成后打开主窗口。nil 表示不开;空串表示开上次看的页面。
|
|
93
|
+
@property(copy) NSString *launchWindowPage;
|
|
79
94
|
@property BOOL pocketHovering;
|
|
80
95
|
@property BOOL dashboardHovering;
|
|
81
96
|
@property BOOL petDragging;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// AppDelegate 主干:启动、退出、窗口跟随。各功能模块在 CCPetsAppDelegate+<模块>.m 里,
|
|
2
2
|
// 共享常量和跨文件的方法声明见 CCPetsAppDelegate+Private.h。
|
|
3
3
|
#import "CCPetsAppDelegate+Private.h"
|
|
4
|
+
#import "CCPetsOpenCommand.h"
|
|
5
|
+
|
|
6
|
+
@implementation CCPetsBubbleContainerView
|
|
7
|
+
@end
|
|
4
8
|
|
|
5
9
|
@implementation CCPetsStatusClickButton
|
|
6
10
|
- (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
|
|
@@ -61,24 +65,34 @@
|
|
|
61
65
|
[alert addButtonWithTitle:L(@"OK")];
|
|
62
66
|
[alert runModal];
|
|
63
67
|
}
|
|
64
|
-
//
|
|
68
|
+
// 主菜单:应用、编辑、窗口三项。
|
|
65
69
|
//
|
|
66
70
|
// ⌘C/⌘V/⌘A/⌘Z 不是 NSTextView 自己处理的:按键先走 NSApp.mainMenu 的
|
|
67
71
|
// performKeyEquivalent:,命中菜单项之后才沿响应者链发 copy: / paste: 这些消息。
|
|
68
|
-
// 这个 app 是 LSUIElement
|
|
72
|
+
// 这个 app 是 LSUIElement,以前没设主菜单时,台词编辑器里这些快捷键全部落空。
|
|
69
73
|
//
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
74
|
+
// 平时是 Accessory,菜单栏不显示,这份菜单只是一张快捷键路由表;主窗口打开期间切成 Regular,
|
|
75
|
+
// 它就是用户看到的菜单栏。第一项按 AppKit 约定当作应用菜单,标题不显示,显示的是 App 名。
|
|
76
|
+
// 桌宠面板是 NSNonactivatingPanel,点它根本不激活 app,碰不到这里。
|
|
73
77
|
//
|
|
74
|
-
// action 一律留给响应者链(target 为 nil),谁是第一响应者谁处理——写死 target 的话
|
|
75
|
-
//
|
|
76
|
-
- (void)
|
|
78
|
+
// 编辑项的 action 一律留给响应者链(target 为 nil),谁是第一响应者谁处理——写死 target 的话
|
|
79
|
+
// 编辑器之外的文本框就用不上了。没有「隐藏」:hide: 会把桌宠浮窗一起藏起来。
|
|
80
|
+
- (void)installMainMenu {
|
|
77
81
|
if (NSApp.mainMenu) return;
|
|
78
82
|
NSMenu *mainMenu = [NSMenu new];
|
|
83
|
+
|
|
84
|
+
NSMenuItem *appItem = [mainMenu addItemWithTitle:@"CC Pets" action:nil keyEquivalent:@""];
|
|
85
|
+
NSMenu *appMenu = [[NSMenu alloc] initWithTitle:@"CC Pets"];
|
|
86
|
+
[appMenu addItemWithTitle:L(@"About CC Pets") action:@selector(showAboutPanel:) keyEquivalent:@""].target = self;
|
|
87
|
+
[appMenu addItem:NSMenuItem.separatorItem];
|
|
88
|
+
[appMenu addItemWithTitle:L(@"Settings…") action:@selector(openMainWindow:) keyEquivalent:@","].target = self;
|
|
89
|
+
[appMenu addItemWithTitle:L(@"Multi-Account") action:@selector(openAccountsPage:) keyEquivalent:@""].target = self;
|
|
90
|
+
[appMenu addItem:NSMenuItem.separatorItem];
|
|
91
|
+
[appMenu addItemWithTitle:L(@"Quit CC Pets") action:@selector(terminate:) keyEquivalent:@"q"].target = NSApp;
|
|
92
|
+
appItem.submenu = appMenu;
|
|
93
|
+
|
|
79
94
|
NSMenuItem *editItem = [mainMenu addItemWithTitle:L(@"Edit") action:nil keyEquivalent:@""];
|
|
80
95
|
NSMenu *editMenu = [[NSMenu alloc] initWithTitle:L(@"Edit")];
|
|
81
|
-
|
|
82
96
|
NSArray<NSArray *> *entries = @[
|
|
83
97
|
@[L(@"Undo"), NSStringFromSelector(@selector(undo:)), @"z", @(NSEventModifierFlagCommand)],
|
|
84
98
|
@[L(@"Redo"), NSStringFromSelector(@selector(redo:)), @"z",
|
|
@@ -100,11 +114,79 @@
|
|
|
100
114
|
item.keyEquivalentModifierMask = (NSEventModifierFlags)[entry[3] unsignedIntegerValue];
|
|
101
115
|
}
|
|
102
116
|
editItem.submenu = editMenu;
|
|
117
|
+
|
|
118
|
+
NSMenuItem *windowItem = [mainMenu addItemWithTitle:L(@"Window") action:nil keyEquivalent:@""];
|
|
119
|
+
NSMenu *windowMenu = [[NSMenu alloc] initWithTitle:L(@"Window")];
|
|
120
|
+
[windowMenu addItemWithTitle:L(@"Minimize") action:@selector(performMiniaturize:) keyEquivalent:@"m"];
|
|
121
|
+
[windowMenu addItemWithTitle:L(@"Close") action:@selector(performClose:) keyEquivalent:@"w"];
|
|
122
|
+
windowItem.submenu = windowMenu;
|
|
123
|
+
NSApp.windowsMenu = windowMenu;
|
|
124
|
+
|
|
103
125
|
NSApp.mainMenu = mainMenu;
|
|
104
126
|
}
|
|
127
|
+
- (void)openMainWindow:(id)sender {
|
|
128
|
+
[CCPetsMainWindowController presentPage:nil];
|
|
129
|
+
}
|
|
130
|
+
- (void)openAccountsPage:(id)sender {
|
|
131
|
+
[CCPetsMainWindowController presentPage:CCPetsMainWindowPageAccounts];
|
|
132
|
+
}
|
|
133
|
+
- (void)openWindowRequested:(NSNotification *)notification {
|
|
134
|
+
NSString *page = notification.userInfo[@"page"];
|
|
135
|
+
[CCPetsMainWindowController presentPage:[page isKindOfClass:NSString.class] && page.length > 0 ? page : nil];
|
|
136
|
+
}
|
|
137
|
+
- (NSDictionary *)petCardSnapshot {
|
|
138
|
+
NSMutableDictionary *snapshot = [NSMutableDictionary dictionary];
|
|
139
|
+
snapshot[@"sheet"] = self.petView.sheet;
|
|
140
|
+
snapshot[@"rowCount"] = @(self.petView.spriteRowCount);
|
|
141
|
+
NSDictionary *option = [self petOptionWithID:self.petView.currentPetID inOptions:[self petOptions]];
|
|
142
|
+
snapshot[@"name"] = [option[@"name"] isKindOfClass:NSString.class] ? option[@"name"] : nil;
|
|
143
|
+
NSString *state = self.hasAgentStatus && self.lastStatusState.length > 0 ? self.lastStatusState : @"idle";
|
|
144
|
+
snapshot[@"state"] = state;
|
|
145
|
+
// 和状态卡标题同一句("Claude · 思考中"),没有会话时就是待机。
|
|
146
|
+
snapshot[@"status"] = self.hasAgentStatus && self.statusTitleLabel.stringValue.length > 0
|
|
147
|
+
? self.statusTitleLabel.stringValue : L(@"Idle");
|
|
148
|
+
snapshot[@"statusColor"] = [self statusColorForState:state];
|
|
149
|
+
// 宠物口吻的那句(状态卡副行),概览页拿来当宠物头上的气泡。
|
|
150
|
+
if (self.hasAgentStatus && self.statusDetailLabel.stringValue.length > 0) {
|
|
151
|
+
snapshot[@"detail"] = self.statusDetailLabel.stringValue;
|
|
152
|
+
}
|
|
153
|
+
snapshot[@"approvals"] = @([self pendingApprovalSessionRecords].count);
|
|
154
|
+
NSUInteger sessions = 0;
|
|
155
|
+
for (NSDictionary *record in self.agentSessionRecords.allValues) {
|
|
156
|
+
if ([self isAgentSessionRecordLive:record]) sessions++;
|
|
157
|
+
}
|
|
158
|
+
snapshot[@"sessions"] = @(sessions);
|
|
159
|
+
return snapshot;
|
|
160
|
+
}
|
|
161
|
+
// 合并到下一轮 runloop 再发:一条 Agent 事件会同时改状态、会话和审批,没必要让名片刷三遍;
|
|
162
|
+
// 而且取快照时会顺带清理离线会话,清理又会走到这里,同步发会在刷新途中再触发刷新。
|
|
163
|
+
- (void)notePetCardChanged {
|
|
164
|
+
static BOOL pending;
|
|
165
|
+
if (pending) return;
|
|
166
|
+
pending = YES;
|
|
167
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
168
|
+
pending = NO;
|
|
169
|
+
[NSNotificationCenter.defaultCenter postNotificationName:CCPetsPetCardDidChangeNotification
|
|
170
|
+
object:nil];
|
|
171
|
+
});
|
|
172
|
+
}
|
|
105
173
|
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
|
|
106
174
|
// 没有主菜单的话,台词编辑器里 ⌘C/⌘V/⌘A/⌘Z 全部没反应。
|
|
107
|
-
[self
|
|
175
|
+
[self installMainMenu];
|
|
176
|
+
// 迁移旧集成,并自动登记官方 ~/.claude / ~/.codex。其他目录永远不自动扫描。
|
|
177
|
+
NSString *profileMigrationError = nil;
|
|
178
|
+
if (!CCPetsMigrateManagedDefaultProfiles(&profileMigrationError) && profileMigrationError.length > 0) {
|
|
179
|
+
fprintf(stderr, "%s\n", profileMigrationError.UTF8String);
|
|
180
|
+
}
|
|
181
|
+
profileMigrationError = nil;
|
|
182
|
+
if (!CCPetsEnsureOfficialDefaultProfiles(&profileMigrationError) && profileMigrationError.length > 0) {
|
|
183
|
+
fprintf(stderr, "%s\n", profileMigrationError.UTF8String);
|
|
184
|
+
}
|
|
185
|
+
// `cc-pets open` 在桌宠已运行时靠这条通知开窗,不走 reopen 事件:每个终端启动 CLI 都会
|
|
186
|
+
// `open -g` 一次桌宠,借 reopen 开窗会让每开一个终端就弹一次主窗口。
|
|
187
|
+
[NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(openWindowRequested:)
|
|
188
|
+
name:CCPetsOpenWindowNotificationName() object:nil
|
|
189
|
+
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
|
|
108
190
|
// 台词全在这个文件里,代码里没有第二份。首次启动先从默认词库拷一份出来,
|
|
109
191
|
// 否则新装的桌宠一句话都不会说。
|
|
110
192
|
PetPhrasesEnsureFileExists();
|
|
@@ -154,7 +236,8 @@
|
|
|
154
236
|
[defaults setBool:YES forKey:StatusBubblePreferenceV2Key];
|
|
155
237
|
}
|
|
156
238
|
self.statusBubbleExpanded = [defaults boolForKey:StatusBubbleExpandedKey];
|
|
157
|
-
|
|
239
|
+
PetMigrateRenamedBuiltinSprite(defaults);
|
|
240
|
+
NSString *selected = [defaults stringForKey:PetSelectedSpriteKey];
|
|
158
241
|
if (selected.length > 0 && ![selected containsString:@":"]) selected = [@"builtin:" stringByAppendingString:selected];
|
|
159
242
|
NSDictionary *selectedOption = [self petOptionWithID:selected inOptions:petOptions] ?: petOptions.firstObject;
|
|
160
243
|
selected = selectedOption[@"id"];
|
|
@@ -406,6 +489,8 @@
|
|
|
406
489
|
self.pendingApprovalRecords = [NSMutableDictionary dictionary];
|
|
407
490
|
self.stallNotifiedSessionKeys = [NSMutableSet set];
|
|
408
491
|
self.usageMonitor.changeHandler = ^(NSDictionary *codexUsage, NSDictionary *claudeUsage) {
|
|
492
|
+
weakSelf.quotaView.profiles = weakSelf.usageMonitor.profiles;
|
|
493
|
+
weakSelf.quotaView.profileUsages = weakSelf.usageMonitor.profileUsages;
|
|
409
494
|
[weakSelf applyCodexUsage:codexUsage claudeUsage:claudeUsage];
|
|
410
495
|
[weakSelf considerQuotaSpeech];
|
|
411
496
|
};
|
|
@@ -442,11 +527,22 @@
|
|
|
442
527
|
name:NSWorkspaceScreensDidWakeNotification object:nil];
|
|
443
528
|
// 启动先让界面和事件流就位,再联网检查更新,别和启动抢时间。
|
|
444
529
|
[self performSelector:@selector(silentCheckForUpdate) withObject:nil afterDelay:5.0];
|
|
530
|
+
if (self.launchWindowPage) {
|
|
531
|
+
[CCPetsMainWindowController presentPage:self.launchWindowPage.length > 0 ? self.launchWindowPage : nil];
|
|
532
|
+
}
|
|
445
533
|
}
|
|
446
534
|
// CLI 启动时桌宠若已在运行,`open -g` 不会重新启动它,只会发一个 reopen 事件过来。
|
|
447
535
|
// 借这个时机同样静默检查一次更新(silentCheckForUpdate 自己做节流)。
|
|
536
|
+
//
|
|
537
|
+
// 主窗口最小化后点 Dock 图标,来的也是 reopen。要把窗口恢复出来,但不能对 `open -g` 这么做:
|
|
538
|
+
// 每开一个终端都会发一次,窗口会被反复弹出来。两者的区别是点 Dock 会激活 App、`open -g` 不会,
|
|
539
|
+
// 所以等到下一轮 runloop(激活已经完成)再看 App 是否在前台。返回 NO:系统默认的处理会去开
|
|
540
|
+
// "新窗口",桌宠没有那种窗口。
|
|
448
541
|
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag {
|
|
449
542
|
[self silentCheckForUpdate];
|
|
543
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
544
|
+
if (NSApp.isActive) [CCPetsMainWindowController restoreFromDock];
|
|
545
|
+
});
|
|
450
546
|
return NO;
|
|
451
547
|
}
|
|
452
548
|
- (void)screensDidSleep:(NSNotification *)notification {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// `cc-pets open [page]`:从终端打开主窗口并定位到页面。
|
|
4
|
+
//
|
|
5
|
+
// 桌宠已在运行:发分布式通知,由它开窗。不能借 reopen 事件——每个终端启动 CLI 都会
|
|
6
|
+
// `open -g` 一次桌宠,借它开窗会让每开一个终端就弹一次主窗口。
|
|
7
|
+
// 桌宠没在运行:不带 -g 地启动 App 并传 `--open-window [page]`,起来后自己开窗。
|
|
8
|
+
//
|
|
9
|
+
// arguments 是 "open" 之后的参数。返回进程退出码。
|
|
10
|
+
int CCPetsOpenMain(NSArray<NSString *> *arguments);
|
|
11
|
+
|
|
12
|
+
// 通知名带 uid:同一台机器上多个用户会话各有各的桌宠,别互相开窗。userInfo 的 page 为空串表示上次的页面。
|
|
13
|
+
NSString *CCPetsOpenWindowNotificationName(void);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#import "CCPetsOpenCommand.h"
|
|
2
|
+
#import "CCPetsCLISupport.h"
|
|
3
|
+
#import "CCPetsL10n.h"
|
|
4
|
+
#import "CCPetsMainWindow.h"
|
|
5
|
+
#import "CCPetsPaths.h"
|
|
6
|
+
#import <sys/file.h>
|
|
7
|
+
#import <sys/stat.h>
|
|
8
|
+
#import <fcntl.h>
|
|
9
|
+
#import <unistd.h>
|
|
10
|
+
|
|
11
|
+
NSString *CCPetsOpenWindowNotificationName(void) {
|
|
12
|
+
return [NSString stringWithFormat:@"com.ccpets.open-main-window.%u", getuid()];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 桌宠是单实例,靠 flock 锁 cc-pets-<uid>.lock;锁被占着就是在运行。探测时拿到锁要马上放掉。
|
|
16
|
+
static BOOL PetIsRunning(void) {
|
|
17
|
+
NSString *lockPath = [PetStateDirectory() stringByAppendingPathComponent:
|
|
18
|
+
[NSString stringWithFormat:@"cc-pets-%u.lock", getuid()]];
|
|
19
|
+
int lock = open(lockPath.fileSystemRepresentation, O_RDONLY);
|
|
20
|
+
// 锁文件不存在说明桌宠从没跑过;这里不创建,探测不该留下痕迹。
|
|
21
|
+
if (lock < 0) return NO;
|
|
22
|
+
BOOL running = flock(lock, LOCK_SH | LOCK_NB) != 0;
|
|
23
|
+
close(lock);
|
|
24
|
+
return running;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 要启动的 App:优先 ~/Applications 里装好的那份(和 bin/cc-pets 一致),其次包目录里构建出来的。
|
|
28
|
+
static NSString *PetApplicationPath(void) {
|
|
29
|
+
NSString *applications = CCPetsEnvironmentValue(@"CC_PETS_APPLICATIONS_DIR") ?:
|
|
30
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@"Applications"];
|
|
31
|
+
NSString *installed = [applications stringByAppendingPathComponent:@"CC Pets.app"];
|
|
32
|
+
if ([NSFileManager.defaultManager fileExistsAtPath:installed]) return installed;
|
|
33
|
+
NSString *executable = CCPetsExecutablePath();
|
|
34
|
+
NSRange built = [executable rangeOfString:@"/.build/release/"];
|
|
35
|
+
if (built.location == NSNotFound) return nil;
|
|
36
|
+
NSString *app = [[executable substringToIndex:NSMaxRange(built)] stringByAppendingPathComponent:@"CC Pets.app"];
|
|
37
|
+
return [NSFileManager.defaultManager fileExistsAtPath:app] ? app : nil;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static NSString *PageList(void) {
|
|
41
|
+
return [CCPetsMainWindowPageIDs() componentsJoinedByString:L(@", ")];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static NSString *UsageText(void) {
|
|
45
|
+
return [NSString stringWithFormat:@"%@\n\n%@", L(@"Usage: cc-pets open [page]"),
|
|
46
|
+
CCPetsInterpolate(L(@"Pages: {pages}"), @{@"pages": PageList()})];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
int CCPetsOpenMain(NSArray<NSString *> *arguments) {
|
|
50
|
+
if ([arguments containsObject:@"--help"] || [arguments containsObject:@"-h"]) {
|
|
51
|
+
CCPetsPrintLine(stdout, UsageText());
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
if (arguments.count > 1) {
|
|
55
|
+
CCPetsPrintLine(stderr, UsageText());
|
|
56
|
+
return 2;
|
|
57
|
+
}
|
|
58
|
+
NSString *page = arguments.firstObject ?: @"";
|
|
59
|
+
if (page.length > 0) {
|
|
60
|
+
// 旧名字(settings)换成现在的页面再往下传,桌宠那边只认新名字。
|
|
61
|
+
NSString *resolved = CCPetsMainWindowResolvePage(page);
|
|
62
|
+
if (!resolved) {
|
|
63
|
+
CCPetsPrintLine(stderr, CCPetsInterpolate(L(@"Unknown page: {page}\nPages: {pages}"),
|
|
64
|
+
@{@"page": page, @"pages": PageList()}));
|
|
65
|
+
return 2;
|
|
66
|
+
}
|
|
67
|
+
page = resolved;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (PetIsRunning()) {
|
|
71
|
+
[NSDistributedNotificationCenter.defaultCenter postNotificationName:CCPetsOpenWindowNotificationName()
|
|
72
|
+
object:nil userInfo:@{@"page": page} deliverImmediately:YES];
|
|
73
|
+
// 通知是异步交给 distnoted 的,进程立刻退出可能发不出去。
|
|
74
|
+
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
NSString *app = PetApplicationPath();
|
|
79
|
+
if (!app) {
|
|
80
|
+
CCPetsPrintLine(stderr, L(@"CC Pets.app not found. Run cc-pets install first."));
|
|
81
|
+
return 1;
|
|
82
|
+
}
|
|
83
|
+
NSMutableArray<NSString *> *openArguments = [NSMutableArray arrayWithObjects:app, @"--args", @"--open-window", nil];
|
|
84
|
+
if (page.length > 0) [openArguments addObject:page];
|
|
85
|
+
NSTask *task = [NSTask new];
|
|
86
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/open"];
|
|
87
|
+
task.arguments = openArguments;
|
|
88
|
+
// CLAUDE_CONFIG_DIR 是会话级的,泄漏给常驻的桌宠会让它永久只统计这个账号(bin/cc-pets 同理)。
|
|
89
|
+
NSMutableDictionary *environment = [NSProcessInfo.processInfo.environment mutableCopy];
|
|
90
|
+
[environment removeObjectForKey:@"CLAUDE_CONFIG_DIR"];
|
|
91
|
+
task.environment = environment;
|
|
92
|
+
NSError *error = nil;
|
|
93
|
+
if (![task launchAndReturnError:&error]) {
|
|
94
|
+
CCPetsPrintLine(stderr, error.localizedDescription);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
[task waitUntilExit];
|
|
98
|
+
return task.terminationStatus;
|
|
99
|
+
}
|
|
@@ -9,7 +9,14 @@
|
|
|
9
9
|
#import "CCPetsUsage.h"
|
|
10
10
|
#import "CCPetsVersion.h"
|
|
11
11
|
#import "CCPetsCleanup.h"
|
|
12
|
+
#import "CCPetsIntegrations.h"
|
|
13
|
+
#import "CCPetsBridgeCLI.h"
|
|
14
|
+
#import "CCPetsDoctor.h"
|
|
15
|
+
#import "CCPetsOpenCommand.h"
|
|
16
|
+
#import "CCPetsPetStore.h"
|
|
17
|
+
#import "CCPetsCLISupport.h"
|
|
12
18
|
#import "CCPetsTerminalFocus.h"
|
|
19
|
+
#import "CCPetsProfiles.h"
|
|
13
20
|
#import <sys/file.h>
|
|
14
21
|
#import <sys/stat.h>
|
|
15
22
|
#import <fcntl.h>
|
|
@@ -17,6 +24,46 @@
|
|
|
17
24
|
|
|
18
25
|
int main(int argc, const char *argv[]) {
|
|
19
26
|
@autoreleasepool {
|
|
27
|
+
// 下面这些都是命令行入口,输出语言跟终端走;真正启动桌宠前再切回 App 的语言规则。
|
|
28
|
+
CCPetsUseCommandLineLanguage(YES);
|
|
29
|
+
if (argc > 1 && (strcmp(argv[1], "doctor") == 0 || strcmp(argv[1], "paths") == 0)) {
|
|
30
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray array];
|
|
31
|
+
for (int index = 1; index < argc; index++) [arguments addObject:@(argv[index])];
|
|
32
|
+
return CCPetsDoctorMain(arguments);
|
|
33
|
+
}
|
|
34
|
+
if (argc > 1 && strcmp(argv[1], "open") == 0) {
|
|
35
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray array];
|
|
36
|
+
for (int index = 2; index < argc; index++) [arguments addObject:@(argv[index])];
|
|
37
|
+
return CCPetsOpenMain(arguments);
|
|
38
|
+
}
|
|
39
|
+
if (argc > 1 && strcmp(argv[1], "pet") == 0) {
|
|
40
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray array];
|
|
41
|
+
for (int index = 2; index < argc; index++) [arguments addObject:@(argv[index])];
|
|
42
|
+
return CCPetsPetStoreMain(arguments);
|
|
43
|
+
}
|
|
44
|
+
if (argc > 1 && strcmp(argv[1], "profile") == 0) {
|
|
45
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray array];
|
|
46
|
+
for (int index = 2; index < argc; index++) [arguments addObject:@(argv[index])];
|
|
47
|
+
return CCPetsProfileCommandMain(arguments);
|
|
48
|
+
}
|
|
49
|
+
if (argc > 1 && strcmp(argv[1], "--configure-updater") == 0) return ConfigureUpdater();
|
|
50
|
+
// zsh 脚本的文案(scripts/i18n.zsh):--localize <英文原文> [name=value …],与 App 共用一张表。
|
|
51
|
+
if (argc > 2 && strcmp(argv[1], "--localize") == 0) {
|
|
52
|
+
NSMutableDictionary<NSString *, NSString *> *values = [NSMutableDictionary dictionary];
|
|
53
|
+
for (int index = 3; index < argc; index++) {
|
|
54
|
+
NSString *pair = @(argv[index]);
|
|
55
|
+
NSRange equals = [pair rangeOfString:@"="];
|
|
56
|
+
if (equals.location == NSNotFound) values[pair] = @"";
|
|
57
|
+
else values[[pair substringToIndex:equals.location]] = [pair substringFromIndex:NSMaxRange(equals)];
|
|
58
|
+
}
|
|
59
|
+
puts(CCPetsInterpolate(CCPetsLocalized(@(argv[2])), values).UTF8String);
|
|
60
|
+
return EXIT_SUCCESS;
|
|
61
|
+
}
|
|
62
|
+
if (argc > 1 && strcmp(argv[1], "bridge") == 0) {
|
|
63
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray array];
|
|
64
|
+
for (int index = 2; index < argc; index++) [arguments addObject:@(argv[index])];
|
|
65
|
+
return CCBridgeCommandMain(arguments);
|
|
66
|
+
}
|
|
20
67
|
if (argc > 1 && (strcmp(argv[1], "--version") == 0 ||
|
|
21
68
|
strcmp(argv[1], "-version") == 0 ||
|
|
22
69
|
strcmp(argv[1], "-v") == 0)) {
|
|
@@ -48,6 +95,19 @@ int main(int argc, const char *argv[]) {
|
|
|
48
95
|
strcmp(argv[1], "provider-event") == 0)) return RecordProviderEvent();
|
|
49
96
|
if (argc > 1 && strcmp(argv[1], "--claude-usage") == 0) return RecordClaudeUsage();
|
|
50
97
|
if (argc > 1 && strcmp(argv[1], "--codex-launch") == 0) return RegisterCodexLaunch();
|
|
98
|
+
// 安装器调用:--install-claude-hooks / --install-codex-hooks <包目录>/.build/release/cc-pets。
|
|
99
|
+
if (argc > 1 && strcmp(argv[1], "--install-claude-hooks") == 0) {
|
|
100
|
+
return InstallClaudeHooks(argc > 2 ? @(argv[2]) : nil);
|
|
101
|
+
}
|
|
102
|
+
if (argc > 1 && strcmp(argv[1], "--install-codex-hooks") == 0) {
|
|
103
|
+
return InstallCodexHooks(argc > 2 ? @(argv[2]) : nil);
|
|
104
|
+
}
|
|
105
|
+
// --uninstall-integrations [--prepare-install] [<shellRC>]
|
|
106
|
+
if (argc > 1 && strcmp(argv[1], "--uninstall-integrations") == 0) {
|
|
107
|
+
BOOL preparing = argc > 2 && strcmp(argv[2], "--prepare-install") == 0;
|
|
108
|
+
int index = preparing ? 3 : 2;
|
|
109
|
+
return UninstallIntegrations(preparing, argc > index ? @(argv[index]) : nil);
|
|
110
|
+
}
|
|
51
111
|
if (argc > 1 && strcmp(argv[1], "--clean") == 0) return CleanCCPetsData(NO);
|
|
52
112
|
if (argc > 1 && strcmp(argv[1], "--purge-data") == 0) return CleanCCPetsData(YES);
|
|
53
113
|
if (argc > 1 && strcmp(argv[1], "--paths") == 0) {
|
|
@@ -104,7 +164,7 @@ int main(int argc, const char *argv[]) {
|
|
|
104
164
|
// 单横线的 -psn_* / -NS* / -Apple* 是系统和 Xcode 启动 App 时自己加的。
|
|
105
165
|
for (int index = 1; index < argc; index++) {
|
|
106
166
|
if (strncmp(argv[index], "--", 2) != 0) continue;
|
|
107
|
-
static const char *launchFlags[] = {"--managed", "--standalone", "--preview-dashboard"};
|
|
167
|
+
static const char *launchFlags[] = {"--managed", "--standalone", "--preview-dashboard", "--open-window"};
|
|
108
168
|
BOOL known = NO;
|
|
109
169
|
for (size_t flag = 0; flag < sizeof(launchFlags) / sizeof(launchFlags[0]); flag++) {
|
|
110
170
|
if (strcmp(argv[index], launchFlags[flag]) == 0) known = YES;
|
|
@@ -115,6 +175,7 @@ int main(int argc, const char *argv[]) {
|
|
|
115
175
|
return 2;
|
|
116
176
|
}
|
|
117
177
|
|
|
178
|
+
CCPetsUseCommandLineLanguage(NO);
|
|
118
179
|
NSString *lockName = [NSString stringWithFormat:@"cc-pets-%u.lock", getuid()];
|
|
119
180
|
NSString *lockPath = [PetStateDirectory() stringByAppendingPathComponent:lockName];
|
|
120
181
|
int lock = open(lockPath.fileSystemRepresentation, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
|
@@ -124,6 +185,12 @@ int main(int argc, const char *argv[]) {
|
|
|
124
185
|
NSApplication *app = NSApplication.sharedApplication;
|
|
125
186
|
AppDelegate *delegate = [AppDelegate new];
|
|
126
187
|
app.delegate = delegate;
|
|
188
|
+
// --open-window [page]:page 省略时开上次看的页面。
|
|
189
|
+
for (int index = 1; index < argc; index++) {
|
|
190
|
+
if (strcmp(argv[index], "--open-window") != 0) continue;
|
|
191
|
+
BOOL hasPage = index + 1 < argc && argv[index + 1][0] != '-';
|
|
192
|
+
delegate.launchWindowPage = hasPage ? @(argv[index + 1]) : @"";
|
|
193
|
+
}
|
|
127
194
|
[app setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
|
128
195
|
[app run];
|
|
129
196
|
(void)lock;
|