cc-pets 2.1.1 → 2.2.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 -0
- package/CC_BRIDGE.zh-CN.md +4 -0
- package/CHANGELOG.md +40 -1
- package/CHANGELOG.zh-CN.md +40 -1
- package/CONTRIBUTING.md +54 -0
- package/CONTRIBUTING.zh-CN.md +45 -0
- package/README.md +57 -14
- package/README.zh-CN.md +56 -14
- package/Resources/Info.plist +3 -1
- package/Resources/en.lproj/InfoPlist.strings +1 -0
- package/Resources/phrases.default.en.txt +182 -0
- package/Resources/zh-Hans.lproj/InfoPlist.strings +1 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +332 -0
- package/Sources/CCPets/CCPetsAppDelegate+AgentEvents.m +215 -0
- package/Sources/CCPets/CCPetsAppDelegate+AgentStatus.m +660 -0
- package/Sources/CCPets/CCPetsAppDelegate+Bridge.m +299 -0
- package/Sources/CCPets/CCPetsAppDelegate+Pets.m +185 -0
- package/Sources/CCPets/CCPetsAppDelegate+Private.h +325 -0
- package/Sources/CCPets/CCPetsAppDelegate+Quota.m +216 -0
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +226 -0
- package/Sources/CCPets/CCPetsAppDelegate+Speech.m +392 -0
- package/Sources/CCPets/CCPetsAppDelegate+Update.m +459 -0
- package/Sources/CCPets/CCPetsAppDelegate.h +13 -14
- package/Sources/CCPets/CCPetsAppDelegate.m +68 -2466
- package/Sources/CCPets/CCPetsCleanup.h +4 -0
- package/Sources/CCPets/CCPetsCleanup.m +41 -7
- package/Sources/CCPets/CCPetsEvents.m +4 -3
- package/Sources/CCPets/CCPetsL10n.h +55 -0
- package/Sources/CCPets/CCPetsL10n.m +219 -0
- package/Sources/CCPets/CCPetsPaths.h +0 -1
- package/Sources/CCPets/CCPetsPaths.m +0 -1
- package/Sources/CCPets/CCPetsPhrases.h +7 -1
- package/Sources/CCPets/CCPetsPhrases.m +86 -31
- package/Sources/CCPets/CCPetsPhrasesEditor.m +62 -64
- package/Sources/CCPets/CCPetsPreviews.m +7 -6
- package/Sources/CCPets/CCPetsUsage.h +0 -1
- package/Sources/CCPets/CCPetsUsage.m +0 -3
- package/Sources/CCPets/CCPetsVersion.h +10 -0
- package/Sources/CCPets/CCPetsVersion.m +92 -2
- package/Sources/CCPets/MenuToggleSwitch.m +3 -2
- package/Sources/CCPets/PetView.h +2 -0
- package/Sources/CCPets/PetView.m +115 -74
- package/Sources/CCPets/QuotaDashboardView.m +122 -88
- package/Sources/CCPets/main.m +16 -1
- package/bin/cc-pets +25 -20
- package/bin/claude-with-pet +2 -1
- package/bin/codex-with-pet +2 -1
- package/package.json +5 -3
- package/scripts/bridge/cli.mjs +47 -39
- package/scripts/bridge/core.mjs +28 -27
- package/scripts/bridge/hook.mjs +7 -6
- package/scripts/bridge/install.mjs +42 -19
- package/scripts/bridge/mcp-server.mjs +79 -71
- package/scripts/bridge/options.mjs +7 -6
- package/scripts/bridge/process.mjs +22 -0
- package/scripts/bridge/reservations.mjs +6 -5
- package/scripts/bridge/store.mjs +10 -9
- package/scripts/build.sh +24 -5
- package/scripts/check-l10n.mjs +168 -0
- package/scripts/configure-updater.mjs +4 -3
- package/scripts/doctor.mjs +470 -0
- package/scripts/i18n.mjs +122 -0
- package/scripts/i18n.zsh +66 -0
- package/scripts/install-app.sh +3 -2
- package/scripts/install-claude-hooks.mjs +22 -27
- package/scripts/install-codex-hooks.mjs +10 -15
- package/scripts/install-shell-integration.sh +6 -5
- package/scripts/integration-markers.mjs +51 -0
- package/scripts/locales/zh-Hans.json +366 -0
- package/scripts/pet-store.mjs +47 -45
- package/scripts/uninstall-app.sh +4 -3
- package/scripts/uninstall-integrations.mjs +24 -51
- package/scripts/uninstall-shell-integration.sh +8 -7
- /package/Resources/{phrases.default.txt → phrases.default.zh-Hans.txt} +0 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// 检查更新、自动更新、更新气泡 / 角标 / 弹窗,以及「关于」。
|
|
2
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
3
|
+
|
|
4
|
+
static void TrimUpdateLog(NSString *path) {
|
|
5
|
+
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
|
|
6
|
+
if (!handle) return;
|
|
7
|
+
unsigned long long size = [handle seekToEndOfFile];
|
|
8
|
+
if (size <= UpdateLogSizeLimit) {
|
|
9
|
+
[handle closeFile];
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
[handle seekToFileOffset:size - UpdateLogSizeLimit];
|
|
13
|
+
NSData *tail = [handle readDataToEndOfFile];
|
|
14
|
+
[handle closeFile];
|
|
15
|
+
[tail writeToFile:path options:NSDataWritingAtomic error:nil];
|
|
16
|
+
chmod(path.fileSystemRepresentation, S_IRUSR | S_IWUSR);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@implementation AppDelegate (Update)
|
|
20
|
+
- (NSDictionary *)updaterConfiguration {
|
|
21
|
+
NSString *path = [[self applicationSupportDirectory] stringByAppendingPathComponent:@"updater.json"];
|
|
22
|
+
NSData *data = [NSData dataWithContentsOfFile:path];
|
|
23
|
+
NSDictionary *configuration = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
24
|
+
return [configuration isKindOfClass:NSDictionary.class] ? configuration : nil;
|
|
25
|
+
}
|
|
26
|
+
- (BOOL)restartAfterUpdateToVersion:(NSString *)version configuration:(NSDictionary *)configuration {
|
|
27
|
+
NSString *appPath = [configuration[@"appPath"] isKindOfClass:NSString.class]
|
|
28
|
+
? [configuration[@"appPath"] stringByStandardizingPath] : nil;
|
|
29
|
+
if (appPath.length == 0 &&
|
|
30
|
+
[NSBundle.mainBundle.bundlePath.pathExtension.lowercaseString isEqualToString:@"app"]) {
|
|
31
|
+
appPath = NSBundle.mainBundle.bundlePath.stringByStandardizingPath;
|
|
32
|
+
}
|
|
33
|
+
if (appPath.length == 0) {
|
|
34
|
+
appPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Applications"]
|
|
35
|
+
stringByAppendingPathComponent:@"CC Pets.app"];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
NSString *infoPath = [appPath stringByAppendingPathComponent:@"Contents/Info.plist"];
|
|
39
|
+
NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:infoPath];
|
|
40
|
+
NSString *installedVersion = [info[@"CFBundleShortVersionString"] isKindOfClass:NSString.class]
|
|
41
|
+
? info[@"CFBundleShortVersionString"] : nil;
|
|
42
|
+
NSString *executablePath = [appPath stringByAppendingPathComponent:@"Contents/MacOS/cc-pets"];
|
|
43
|
+
if (![installedVersion isEqualToString:version] ||
|
|
44
|
+
![NSFileManager.defaultManager isExecutableFileAtPath:executablePath]) return NO;
|
|
45
|
+
|
|
46
|
+
NSTask *restartTask = [NSTask new];
|
|
47
|
+
restartTask.executableURL = [NSURL fileURLWithPath:executablePath];
|
|
48
|
+
restartTask.arguments = @[
|
|
49
|
+
@"--restart-after-pid",
|
|
50
|
+
[NSString stringWithFormat:@"%d", NSProcessInfo.processInfo.processIdentifier],
|
|
51
|
+
appPath,
|
|
52
|
+
self.managedByCLI ? @"--managed" : @"--standalone"
|
|
53
|
+
];
|
|
54
|
+
NSError *error = nil;
|
|
55
|
+
if (![restartTask launchAndReturnError:&error]) return NO;
|
|
56
|
+
[NSApp terminate:nil];
|
|
57
|
+
return YES;
|
|
58
|
+
}
|
|
59
|
+
- (void)startUpdateToVersion:(NSString *)version {
|
|
60
|
+
if (self.updating) return;
|
|
61
|
+
[self startUpdateToVersion:version attempt:0];
|
|
62
|
+
}
|
|
63
|
+
- (void)retryUpdate:(NSArray *)context {
|
|
64
|
+
self.updating = NO;
|
|
65
|
+
[self refreshUpdateBadge];
|
|
66
|
+
[self startUpdateToVersion:context[0] attempt:[context[1] integerValue]];
|
|
67
|
+
}
|
|
68
|
+
- (void)startUpdateToVersion:(NSString *)version attempt:(NSInteger)attempt {
|
|
69
|
+
NSDictionary *configuration = [self updaterConfiguration];
|
|
70
|
+
NSString *nodePath = [configuration[@"nodePath"] isKindOfClass:NSString.class]
|
|
71
|
+
? [configuration[@"nodePath"] stringByStandardizingPath] : nil;
|
|
72
|
+
NSString *npmCliPath = [configuration[@"npmCliPath"] isKindOfClass:NSString.class]
|
|
73
|
+
? [configuration[@"npmCliPath"] stringByStandardizingPath] : nil;
|
|
74
|
+
BOOL nodeExecutable = nodePath.isAbsolutePath &&
|
|
75
|
+
[NSFileManager.defaultManager isExecutableFileAtPath:nodePath];
|
|
76
|
+
BOOL npmCliExists = npmCliPath.isAbsolutePath &&
|
|
77
|
+
[NSFileManager.defaultManager isReadableFileAtPath:npmCliPath];
|
|
78
|
+
if (!nodeExecutable || !npmCliExists) {
|
|
79
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically")
|
|
80
|
+
message:L(@"The Node.js/npm used to install CC Pets wasn't found. Run this once manually:\n"
|
|
81
|
+
"\n"
|
|
82
|
+
"npm install -g cc-pets@latest --allow-scripts=cc-pets")];
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
NSString *supportDirectory = [self applicationSupportDirectory];
|
|
87
|
+
NSError *directoryError = nil;
|
|
88
|
+
[NSFileManager.defaultManager createDirectoryAtPath:supportDirectory
|
|
89
|
+
withIntermediateDirectories:YES attributes:nil error:&directoryError];
|
|
90
|
+
if (directoryError) {
|
|
91
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically") message:directoryError.localizedDescription];
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
NSString *logPath = [supportDirectory stringByAppendingPathComponent:@"update.log"];
|
|
95
|
+
[NSData.data writeToFile:logPath options:NSDataWritingAtomic error:nil];
|
|
96
|
+
chmod(logPath.fileSystemRepresentation, S_IRUSR | S_IWUSR);
|
|
97
|
+
NSFileHandle *logHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
|
|
98
|
+
if (!logHandle) {
|
|
99
|
+
[self showAlertWithTitle:L(@"Can't Update Automatically") message:L(@"Couldn't create the update log.")];
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
NSTask *task = [NSTask new];
|
|
104
|
+
task.executableURL = [NSURL fileURLWithPath:nodePath];
|
|
105
|
+
NSMutableArray<NSString *> *arguments = [@[
|
|
106
|
+
npmCliPath,
|
|
107
|
+
@"install",
|
|
108
|
+
@"--global",
|
|
109
|
+
[@"cc-pets@" stringByAppendingString:version],
|
|
110
|
+
@"--allow-scripts=cc-pets",
|
|
111
|
+
@"--prefer-online"
|
|
112
|
+
] mutableCopy];
|
|
113
|
+
if (attempt > 0) {
|
|
114
|
+
// 重试走一个一次性缓存目录:ETARGET 的成因就是本机缓存里的包元数据还没有这个
|
|
115
|
+
// 版本,--prefer-online 只是允许重新校验,命中 304 时依然拿到旧元数据。
|
|
116
|
+
// 换缓存目录能强制冷取,又不动用户真正的 npm 缓存。
|
|
117
|
+
[arguments addObjectsFromArray:@[@"--cache",
|
|
118
|
+
[supportDirectory stringByAppendingPathComponent:@"update-retry-cache"]]];
|
|
119
|
+
}
|
|
120
|
+
task.arguments = arguments;
|
|
121
|
+
NSMutableDictionary<NSString *, NSString *> *environment =
|
|
122
|
+
[NSProcessInfo.processInfo.environment mutableCopy];
|
|
123
|
+
NSString *nodeDirectory = nodePath.stringByDeletingLastPathComponent;
|
|
124
|
+
NSString *existingPath = environment[@"PATH"];
|
|
125
|
+
if (existingPath.length == 0) existingPath = @"/usr/bin:/bin:/usr/sbin:/sbin";
|
|
126
|
+
environment[@"PATH"] = [NSString stringWithFormat:@"%@:%@", nodeDirectory, existingPath];
|
|
127
|
+
task.environment = environment;
|
|
128
|
+
task.currentDirectoryURL = [NSURL fileURLWithPath:NSHomeDirectory()];
|
|
129
|
+
task.standardOutput = logHandle;
|
|
130
|
+
task.standardError = logHandle;
|
|
131
|
+
self.updating = YES;
|
|
132
|
+
[self refreshUpdateBadge];
|
|
133
|
+
self.updateTask = task;
|
|
134
|
+
__weak typeof(self) weakSelf = self;
|
|
135
|
+
task.terminationHandler = ^(NSTask *finishedTask) {
|
|
136
|
+
[logHandle closeFile];
|
|
137
|
+
TrimUpdateLog(logPath);
|
|
138
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
139
|
+
typeof(self) strongSelf = weakSelf;
|
|
140
|
+
if (!strongSelf) return;
|
|
141
|
+
strongSelf.updateTask = nil;
|
|
142
|
+
if (finishedTask.terminationStatus == EXIT_SUCCESS) {
|
|
143
|
+
strongSelf.updating = NO;
|
|
144
|
+
[strongSelf refreshUpdateBadge];
|
|
145
|
+
if ([strongSelf restartAfterUpdateToVersion:version configuration:configuration]) return;
|
|
146
|
+
[strongSelf showAlertWithTitle:L(@"Update Complete")
|
|
147
|
+
message:L(@"CC Pets was updated, but the new app couldn't be relaunched automatically. Please restart it manually.")];
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
// 暂时性故障(典型是刚发布的版本报 ETARGET)自动重试一次再说,
|
|
151
|
+
// 别把一个重试就能过的问题弹成"更新失败"。updating 保持为 YES,
|
|
152
|
+
// 等待期间不接受新的更新请求。
|
|
153
|
+
NSString *log = [NSString stringWithContentsOfFile:logPath
|
|
154
|
+
encoding:NSUTF8StringEncoding error:nil];
|
|
155
|
+
if (attempt == 0 && UpdateFailureIsTransient(log)) {
|
|
156
|
+
[strongSelf performSelector:@selector(retryUpdate:)
|
|
157
|
+
withObject:@[version, @1] afterDelay:UpdateRetryDelay];
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
strongSelf.updating = NO;
|
|
161
|
+
[strongSelf refreshUpdateBadge];
|
|
162
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
163
|
+
NSAlert *alert = [NSAlert new];
|
|
164
|
+
alert.messageText = L(@"Update Failed");
|
|
165
|
+
alert.informativeText = [NSString stringWithFormat:
|
|
166
|
+
L(@"You can keep using the current version. Details were written to:\n%@"), logPath];
|
|
167
|
+
[alert addButtonWithTitle:L(@"Open Log")];
|
|
168
|
+
[alert addButtonWithTitle:L(@"Close")];
|
|
169
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
170
|
+
[NSWorkspace.sharedWorkspace openURL:[NSURL fileURLWithPath:logPath]];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
NSError *launchError = nil;
|
|
175
|
+
if (![task launchAndReturnError:&launchError]) {
|
|
176
|
+
self.updating = NO;
|
|
177
|
+
[self refreshUpdateBadge];
|
|
178
|
+
self.updateTask = nil;
|
|
179
|
+
[logHandle closeFile];
|
|
180
|
+
[self showAlertWithTitle:L(@"Can't Start Update") message:launchError.localizedDescription];
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// 重试是静默的:第一次已经弹过"正在更新",再弹一次只会让人以为出了两回事。
|
|
184
|
+
if (attempt == 0) {
|
|
185
|
+
[self showAlertWithTitle:L(@"Updating")
|
|
186
|
+
message:[NSString stringWithFormat:L(@"Downloading and installing CC Pets %@. The pet will restart when it's done."), version]];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// 关于弹窗与检查更新同用 NSAlert,保持样式一致;版本号用构建时注入的 CC_PETS_VERSION(与检查更新
|
|
190
|
+
// 同一口径)。桌宠是 LSUIElement,不先激活的话弹窗会开在其他 App 后面。
|
|
191
|
+
- (void)showAboutPanel:(id)sender {
|
|
192
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
193
|
+
NSAlert *alert = [NSAlert new];
|
|
194
|
+
alert.messageText = @"CC Pets";
|
|
195
|
+
alert.informativeText = [NSString stringWithFormat:
|
|
196
|
+
L(@"Version %@\n\nA desktop pet for Claude Code and Codex CLI."), @CC_PETS_VERSION];
|
|
197
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
198
|
+
[alert addButtonWithTitle:L(@"Visit Homepage")];
|
|
199
|
+
if ([alert runModal] == NSAlertSecondButtonReturn) {
|
|
200
|
+
[NSWorkspace.sharedWorkspace openURL:
|
|
201
|
+
[NSURL URLWithString:[@"https://github.com/" stringByAppendingString:CCPetsRepositorySlug]]];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// 版本号以 npm Registry 为准(自动更新装的就是它),更新说明取同版本 tag 的 GitHub Release
|
|
205
|
+
// 描述。Release 没写、没建、或 GitHub 请求失败都只是没有说明,不影响提示更新本身。
|
|
206
|
+
- (void)fetchLatestReleaseWithCompletion:(void (^)(NSString *version, NSArray<NSString *> *highlights,
|
|
207
|
+
BOOL highlightsTruncated, NSString *errorMessage))completion {
|
|
208
|
+
NSURL *url = [NSURL URLWithString:@"https://registry.npmjs.org/cc-pets/latest"];
|
|
209
|
+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
|
|
210
|
+
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
|
|
211
|
+
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
|
212
|
+
NSURLSessionDataTask *task = [NSURLSession.sharedSession dataTaskWithRequest:request
|
|
213
|
+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
214
|
+
NSHTTPURLResponse *httpResponse = [response isKindOfClass:NSHTTPURLResponse.class]
|
|
215
|
+
? (NSHTTPURLResponse *)response : nil;
|
|
216
|
+
NSDictionary *metadata = data
|
|
217
|
+
? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
218
|
+
NSString *latestVersion = [metadata isKindOfClass:NSDictionary.class] &&
|
|
219
|
+
[metadata[@"version"] isKindOfClass:NSString.class] ? metadata[@"version"] : nil;
|
|
220
|
+
BOOL valid = NO;
|
|
221
|
+
NSComparisonResult comparison = CompareStableVersions(@CC_PETS_VERSION, latestVersion, &valid);
|
|
222
|
+
if (error || httpResponse.statusCode != 200 || !valid) {
|
|
223
|
+
NSString *message = error.localizedDescription ?: L(@"The npm registry returned invalid version info. Please try again later.");
|
|
224
|
+
dispatch_async(dispatch_get_main_queue(), ^{ completion(nil, nil, NO, message); });
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (comparison != NSOrderedAscending) {
|
|
228
|
+
dispatch_async(dispatch_get_main_queue(), ^{ completion(latestVersion, nil, NO, nil); });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
NSString *releaseURL = [NSString stringWithFormat:
|
|
232
|
+
@"https://api.github.com/repos/%@/releases/tags/v%@", CCPetsRepositorySlug, latestVersion];
|
|
233
|
+
NSMutableURLRequest *releaseRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:releaseURL]
|
|
234
|
+
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
|
|
235
|
+
[releaseRequest setValue:@"application/vnd.github+json" forHTTPHeaderField:@"Accept"];
|
|
236
|
+
[releaseRequest setValue:@"cc-pets/" CC_PETS_VERSION forHTTPHeaderField:@"User-Agent"];
|
|
237
|
+
NSURLSessionDataTask *releaseTask = [NSURLSession.sharedSession dataTaskWithRequest:releaseRequest
|
|
238
|
+
completionHandler:^(NSData *releaseData, NSURLResponse *releaseResponse, NSError *releaseError) {
|
|
239
|
+
NSInteger status = [releaseResponse isKindOfClass:NSHTTPURLResponse.class]
|
|
240
|
+
? ((NSHTTPURLResponse *)releaseResponse).statusCode : 0;
|
|
241
|
+
NSDictionary *release = !releaseError && status == 200 && releaseData
|
|
242
|
+
? [NSJSONSerialization JSONObjectWithData:releaseData options:0 error:nil] : nil;
|
|
243
|
+
NSString *body = [release isKindOfClass:NSDictionary.class] &&
|
|
244
|
+
[release[@"body"] isKindOfClass:NSString.class] ? release[@"body"] : nil;
|
|
245
|
+
BOOL truncated = NO;
|
|
246
|
+
NSArray<NSString *> *highlights = ReleaseNoteHighlightsForLanguage(body,
|
|
247
|
+
CCPetsCurrentLanguage(), UpdateHighlightLimit, UpdateHighlightMaxLength, &truncated);
|
|
248
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
249
|
+
completion(latestVersion, highlights, truncated, nil);
|
|
250
|
+
});
|
|
251
|
+
}];
|
|
252
|
+
[releaseTask resume];
|
|
253
|
+
}];
|
|
254
|
+
[task resume];
|
|
255
|
+
}
|
|
256
|
+
- (void)checkForUpdates:(id)sender {
|
|
257
|
+
if (self.checkingForUpdate || self.updating) return;
|
|
258
|
+
self.checkingForUpdate = YES;
|
|
259
|
+
__weak typeof(self) weakSelf = self;
|
|
260
|
+
[self fetchLatestReleaseWithCompletion:^(NSString *version, NSArray<NSString *> *highlights,
|
|
261
|
+
BOOL truncated, NSString *errorMessage) {
|
|
262
|
+
typeof(self) strongSelf = weakSelf;
|
|
263
|
+
if (!strongSelf) return;
|
|
264
|
+
strongSelf.checkingForUpdate = NO;
|
|
265
|
+
if (errorMessage) {
|
|
266
|
+
[strongSelf showAlertWithTitle:L(@"Update Check Failed") message:errorMessage];
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
BOOL valid = NO;
|
|
270
|
+
if (CompareStableVersions(@CC_PETS_VERSION, version, &valid) != NSOrderedAscending) {
|
|
271
|
+
[strongSelf clearPendingUpdate];
|
|
272
|
+
[strongSelf showAlertWithTitle:L(@"You're Up to Date")
|
|
273
|
+
message:[NSString stringWithFormat:L(@"Version %@"), @CC_PETS_VERSION]];
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
[strongSelf rememberPendingUpdate:version highlights:highlights truncated:truncated];
|
|
277
|
+
[strongSelf showUpdateDialog];
|
|
278
|
+
}];
|
|
279
|
+
}
|
|
280
|
+
// 启动时(含 CLI 再次 open 一个已在运行的桌宠)静默检查一次:失败一律不打扰,
|
|
281
|
+
// 有新版本就让宠物冒一个能点的气泡。距上次联网不到 UpdateSilentCheckInterval 时
|
|
282
|
+
// 不重复请求,已知有新版本的话直接再提示一次。
|
|
283
|
+
- (void)silentCheckForUpdate {
|
|
284
|
+
if (self.checkingForUpdate || self.updating) return;
|
|
285
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
286
|
+
if (self.lastSilentUpdateCheckAt > 0 && now - self.lastSilentUpdateCheckAt < UpdateSilentCheckInterval) {
|
|
287
|
+
if (self.pendingUpdateVersion) [self showUpdateBubble];
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
self.lastSilentUpdateCheckAt = now;
|
|
291
|
+
self.checkingForUpdate = YES;
|
|
292
|
+
__weak typeof(self) weakSelf = self;
|
|
293
|
+
[self fetchLatestReleaseWithCompletion:^(NSString *version, NSArray<NSString *> *highlights,
|
|
294
|
+
BOOL truncated, NSString *errorMessage) {
|
|
295
|
+
typeof(self) strongSelf = weakSelf;
|
|
296
|
+
if (!strongSelf) return;
|
|
297
|
+
strongSelf.checkingForUpdate = NO;
|
|
298
|
+
if (errorMessage || strongSelf.updating) return;
|
|
299
|
+
BOOL valid = NO;
|
|
300
|
+
if (CompareStableVersions(@CC_PETS_VERSION, version, &valid) != NSOrderedAscending) {
|
|
301
|
+
[strongSelf clearPendingUpdate];
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
[strongSelf rememberPendingUpdate:version highlights:highlights truncated:truncated];
|
|
305
|
+
[strongSelf showUpdateBubble];
|
|
306
|
+
}];
|
|
307
|
+
}
|
|
308
|
+
- (void)rememberPendingUpdate:(NSString *)version highlights:(NSArray<NSString *> *)highlights
|
|
309
|
+
truncated:(BOOL)truncated {
|
|
310
|
+
// 「稍后」只针对当时那个版本;又出了更新的版本就重新提醒。
|
|
311
|
+
if (![version isEqualToString:self.pendingUpdateVersion]) self.updateReminderSnoozed = NO;
|
|
312
|
+
self.pendingUpdateVersion = version;
|
|
313
|
+
self.pendingUpdateHighlights = highlights ?: @[];
|
|
314
|
+
self.pendingUpdateHighlightsTruncated = truncated;
|
|
315
|
+
[self refreshUpdateBadge];
|
|
316
|
+
}
|
|
317
|
+
- (void)refreshUpdateBadge {
|
|
318
|
+
BOOL show = self.pendingUpdateVersion.length > 0 && !self.updating;
|
|
319
|
+
self.updateBadgeView.hidden = !show;
|
|
320
|
+
self.updateBadgeButton.toolTip = show
|
|
321
|
+
? [NSString stringWithFormat:L(@"Version %@ is available. Click to view."), self.pendingUpdateVersion] : nil;
|
|
322
|
+
}
|
|
323
|
+
- (void)clearPendingUpdate {
|
|
324
|
+
self.pendingUpdateVersion = nil;
|
|
325
|
+
self.pendingUpdateHighlights = nil;
|
|
326
|
+
self.pendingUpdateHighlightsTruncated = NO;
|
|
327
|
+
[self refreshUpdateBadge];
|
|
328
|
+
if (self.updateBubbleVisible) [self hideSpeechBubble];
|
|
329
|
+
}
|
|
330
|
+
// 更新要点放进 accessoryView 而不是 informativeText:后者是一整段纯文本,列表项折行后
|
|
331
|
+
// 第二行会顶到「•」下面,几条长说明挤成一坨。这里用悬挂缩进让折行对齐到文字起点。
|
|
332
|
+
- (NSView *)updateHighlightsAccessoryView {
|
|
333
|
+
if (self.pendingUpdateHighlights.count == 0) return nil;
|
|
334
|
+
const CGFloat width = 300;
|
|
335
|
+
NSFont *bodyFont = [NSFont systemFontOfSize:12];
|
|
336
|
+
NSString *bullet = @"•\t";
|
|
337
|
+
CGFloat indent = ceil([bullet sizeWithAttributes:@{NSFontAttributeName: bodyFont}].width) + 4;
|
|
338
|
+
NSMutableParagraphStyle *itemStyle = [NSMutableParagraphStyle new];
|
|
339
|
+
itemStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft
|
|
340
|
+
location:indent options:@{}]];
|
|
341
|
+
itemStyle.headIndent = indent;
|
|
342
|
+
itemStyle.paragraphSpacing = 5;
|
|
343
|
+
itemStyle.lineSpacing = 1;
|
|
344
|
+
NSMutableParagraphStyle *headerStyle = [NSMutableParagraphStyle new];
|
|
345
|
+
headerStyle.paragraphSpacing = 6;
|
|
346
|
+
|
|
347
|
+
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:L(@"What's New\n")
|
|
348
|
+
attributes:@{NSFontAttributeName: [NSFont systemFontOfSize:12 weight:NSFontWeightSemibold],
|
|
349
|
+
NSForegroundColorAttributeName: NSColor.labelColor,
|
|
350
|
+
NSParagraphStyleAttributeName: headerStyle}];
|
|
351
|
+
NSDictionary *itemAttributes = @{NSFontAttributeName: bodyFont,
|
|
352
|
+
NSForegroundColorAttributeName: NSColor.secondaryLabelColor,
|
|
353
|
+
NSParagraphStyleAttributeName: itemStyle};
|
|
354
|
+
[self.pendingUpdateHighlights enumerateObjectsUsingBlock:^(NSString *item, NSUInteger index, BOOL *stop) {
|
|
355
|
+
NSString *line = [NSString stringWithFormat:@"%@%@%@", bullet, item,
|
|
356
|
+
index + 1 < self.pendingUpdateHighlights.count || self.pendingUpdateHighlightsTruncated
|
|
357
|
+
? @"\n" : @""];
|
|
358
|
+
[text appendAttributedString:[[NSAttributedString alloc] initWithString:line
|
|
359
|
+
attributes:itemAttributes]];
|
|
360
|
+
}];
|
|
361
|
+
if (self.pendingUpdateHighlightsTruncated) {
|
|
362
|
+
[text appendAttributedString:[[NSAttributedString alloc] initWithString:L(@"More changes in the full GitHub release notes")
|
|
363
|
+
attributes:@{NSFontAttributeName: [NSFont systemFontOfSize:11],
|
|
364
|
+
NSForegroundColorAttributeName: NSColor.tertiaryLabelColor,
|
|
365
|
+
NSParagraphStyleAttributeName: itemStyle}]];
|
|
366
|
+
}
|
|
367
|
+
NSTextField *label = [NSTextField wrappingLabelWithString:@""];
|
|
368
|
+
label.attributedStringValue = text;
|
|
369
|
+
label.preferredMaxLayoutWidth = width;
|
|
370
|
+
NSSize size = [label.cell cellSizeForBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)];
|
|
371
|
+
label.frame = NSMakeRect(0, 0, width, ceil(size.height));
|
|
372
|
+
return label;
|
|
373
|
+
}
|
|
374
|
+
- (void)showUpdateDialog {
|
|
375
|
+
NSString *version = self.pendingUpdateVersion;
|
|
376
|
+
if (version.length == 0 || self.updating) return;
|
|
377
|
+
if (self.updateBubbleVisible) [self hideSpeechBubble];
|
|
378
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
379
|
+
NSAlert *alert = [NSAlert new];
|
|
380
|
+
alert.messageText = [NSString stringWithFormat:L(@"CC Pets %@ Is Available"), version];
|
|
381
|
+
alert.informativeText = [NSString stringWithFormat:L(@"Current version %@"), @CC_PETS_VERSION];
|
|
382
|
+
alert.accessoryView = [self updateHighlightsAccessoryView];
|
|
383
|
+
[alert addButtonWithTitle:L(@"Update Now")];
|
|
384
|
+
[alert addButtonWithTitle:L(@"Later")];
|
|
385
|
+
if (self.pendingUpdateHighlights.count > 0) [alert addButtonWithTitle:L(@"Release Notes")];
|
|
386
|
+
NSModalResponse response = [alert runModal];
|
|
387
|
+
if (response == NSAlertFirstButtonReturn) {
|
|
388
|
+
[self startUpdateToVersion:version];
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (response == NSAlertThirdButtonReturn) {
|
|
392
|
+
[NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:[NSString stringWithFormat:
|
|
393
|
+
@"https://github.com/%@/releases/tag/v%@", CCPetsRepositorySlug, version]]];
|
|
394
|
+
}
|
|
395
|
+
// 用户没选立即更新,本次运行不再主动冒气泡;角标和菜单入口照旧在。
|
|
396
|
+
self.updateReminderSnoozed = YES;
|
|
397
|
+
self.updateBubbleDeferred = NO;
|
|
398
|
+
}
|
|
399
|
+
// 顶层「更新到 x.y.z…」:版本已知,直接打开更新弹窗,不再联网查一遍。
|
|
400
|
+
- (void)showPendingUpdate:(id)sender {
|
|
401
|
+
[self showUpdateDialog];
|
|
402
|
+
}
|
|
403
|
+
// 正在下载安装时,「帮助 ▸ 检查更新…」置灰,免得用户以为点了没反应。
|
|
404
|
+
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
|
|
405
|
+
if (menuItem.action == @selector(checkForUpdates:)) return !self.updating;
|
|
406
|
+
return YES;
|
|
407
|
+
}
|
|
408
|
+
// 角标箭头与气泡文字同一套规则:清透玻璃上白色加投影,经典磨砂上深色。
|
|
409
|
+
- (void)applyUpdateBadgeStyle {
|
|
410
|
+
if (!self.updateBadgeGlass) return;
|
|
411
|
+
BOOL liquid = self.updateBadgeGlass.usesLiquidGlass;
|
|
412
|
+
self.updateBadgeArrow.contentTintColor = liquid ? [NSColor colorWithWhite:1 alpha:0.97]
|
|
413
|
+
: [NSColor colorWithWhite:0.12 alpha:0.96];
|
|
414
|
+
NSShadow *shadow = [NSShadow new];
|
|
415
|
+
shadow.shadowColor = [NSColor colorWithWhite:0 alpha:0.45];
|
|
416
|
+
shadow.shadowBlurRadius = 2;
|
|
417
|
+
shadow.shadowOffset = NSMakeSize(0, -0.5);
|
|
418
|
+
self.updateBadgeArrow.shadow = liquid ? shadow : nil;
|
|
419
|
+
}
|
|
420
|
+
// 更新提示:不受碎碎念开关、冷却和每小时预算限制。宠物头上始终只挂一个泡:
|
|
421
|
+
// agent 在忙就先记下来,等它闲下来(considerIdleSpeech 每 3 秒一跳)再冒;
|
|
422
|
+
// 状态卡只是在待命时,临时把它收起让位,气泡消失后再放回来。
|
|
423
|
+
- (void)showUpdateBubble {
|
|
424
|
+
if (self.pendingUpdateVersion.length == 0 || self.updating || self.updateReminderSnoozed) {
|
|
425
|
+
self.updateBubbleDeferred = NO;
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if ([self agentBusyForSpeech]) {
|
|
429
|
+
self.updateBubbleDeferred = YES;
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
self.updateBubbleDeferred = NO;
|
|
433
|
+
[self showUpdateBubbleWithText:[NSString stringWithFormat:L(@"CC Pets v%@ is out! Click to update"),
|
|
434
|
+
self.pendingUpdateVersion] dwell:UpdateBubbleDwell];
|
|
435
|
+
}
|
|
436
|
+
// 碎碎念时机里的更新提醒,几句轮换,免得每次都是同一句。
|
|
437
|
+
- (NSString *)updateReminderText {
|
|
438
|
+
NSArray<NSString *> *lines = @[
|
|
439
|
+
L(@"{version} is still waiting. Click me!"),
|
|
440
|
+
L(@"{version} is out. See what's new!"),
|
|
441
|
+
L(@"Upgrade to {version}? Click me"),
|
|
442
|
+
];
|
|
443
|
+
return [lines[arc4random_uniform((uint32_t)lines.count)]
|
|
444
|
+
stringByReplacingOccurrencesOfString:@"{version}" withString:self.pendingUpdateVersion];
|
|
445
|
+
}
|
|
446
|
+
- (void)showUpdateBubbleWithText:(NSString *)text dwell:(NSTimeInterval)dwell {
|
|
447
|
+
if (self.statusPanel.isVisible) {
|
|
448
|
+
[self.statusPanel orderOut:nil];
|
|
449
|
+
self.updateBubbleSuppressedStatus = YES;
|
|
450
|
+
}
|
|
451
|
+
[self showSpeechBubbleWithText:text dwell:dwell];
|
|
452
|
+
self.updateBubbleVisible = YES;
|
|
453
|
+
self.speechPanel.ignoresMouseEvents = NO;
|
|
454
|
+
self.speechClickButton.hidden = NO;
|
|
455
|
+
}
|
|
456
|
+
- (void)updateBubbleClicked:(id)sender {
|
|
457
|
+
[self showUpdateDialog];
|
|
458
|
+
}
|
|
459
|
+
@end
|
|
@@ -92,6 +92,19 @@
|
|
|
92
92
|
@property BOOL agentEventReaderInitialized;
|
|
93
93
|
@property BOOL checkingForUpdate;
|
|
94
94
|
@property BOOL updating;
|
|
95
|
+
@property(copy) NSString *pendingUpdateVersion;
|
|
96
|
+
@property(copy) NSArray<NSString *> *pendingUpdateHighlights;
|
|
97
|
+
@property BOOL pendingUpdateHighlightsTruncated;
|
|
98
|
+
@property NSTimeInterval lastSilentUpdateCheckAt;
|
|
99
|
+
@property BOOL updateBubbleVisible;
|
|
100
|
+
@property BOOL updateBubbleDeferred;
|
|
101
|
+
@property BOOL updateBubbleSuppressedStatus;
|
|
102
|
+
@property BOOL updateReminderSnoozed;
|
|
103
|
+
@property NSButton *updateBadgeButton;
|
|
104
|
+
@property NSView *updateBadgeView;
|
|
105
|
+
@property CCPetsGlassView *updateBadgeGlass;
|
|
106
|
+
@property NSImageView *updateBadgeArrow;
|
|
107
|
+
@property NSButton *speechClickButton;
|
|
95
108
|
@property NSTask *updateTask;
|
|
96
109
|
@property NSTimer *usageTimer;
|
|
97
110
|
// 上一次全量聚合的时刻:面板由悬停触发,短时间内反复进出热区不必每次重算。
|
|
@@ -99,18 +112,4 @@
|
|
|
99
112
|
@property NSTimer *quotaClockTimer;
|
|
100
113
|
@property NSTimer *systemMetricsTimer;
|
|
101
114
|
@property CCPetsSystemMonitor *systemMonitor;
|
|
102
|
-
- (void)showQuotaDashboard;
|
|
103
|
-
- (void)positionQuotaDashboard;
|
|
104
|
-
- (void)scheduleQuotaDashboardHide;
|
|
105
|
-
- (void)hideQuotaDashboardIfNeeded;
|
|
106
|
-
- (void)positionAgentStatus;
|
|
107
|
-
- (void)showAgentStatusForRecord:(NSDictionary *)record;
|
|
108
|
-
- (void)prunePendingApprovalRecords;
|
|
109
|
-
- (void)toggleStatusBubbleFromMenu:(NSButton *)sender;
|
|
110
|
-
- (void)refreshUsage:(id)sender;
|
|
111
|
-
- (void)toggleSystemMetric:(NSButton *)sender;
|
|
112
|
-
- (void)setUsageDisplayModeFromControl:(NSButton *)sender;
|
|
113
|
-
- (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage;
|
|
114
|
-
- (void)switchPetToID:(NSString *)petID;
|
|
115
|
-
- (BOOL)deletePetWithID:(NSString *)petID;
|
|
116
115
|
@end
|