cc-pets 0.2.12 → 0.2.14
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/CHANGELOG.md +2 -1
- package/README.md +9 -3
- package/Sources/CCPets/main.m +262 -0
- package/package.json +1 -1
- package/scripts/configure-updater.mjs +39 -0
- package/scripts/install-shell-integration.sh +1 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
- 鼠标悬停口袋时展开额度详情面板,用两张卡分别显示 Codex、Claude 的剩余 5 小时额度、周额度和精确到秒的周重置时间
|
|
11
11
|
- reset 时间自动转换为 Mac 当前系统时区,例如 `Jul 22 3pm America/Los_Angeles` 在上海时区显示为 `7/23 06:00:00`
|
|
12
12
|
- 支持待机、左右拖动、身体两侧感应、头部/脚部悬停和点击反馈动画
|
|
13
|
-
-
|
|
13
|
+
- 支持拖动桌宠位置,右键刷新用量、检查更新或退出
|
|
14
14
|
- 响应 Codex 与 Claude Code Agent 的思考、工具调用、审批、子 Agent、完成和失败状态
|
|
15
15
|
- 使用原生 AppKit,运行时不依赖 Electron
|
|
16
16
|
|
|
@@ -52,7 +52,7 @@ Claude Code Hook 会合并到 `~/.claude/settings.json`,现有的 `env`、`mod
|
|
|
52
52
|
| 按住并向右拖动 | 播放向右拖行动画,同时移动桌宠 |
|
|
53
53
|
| 按住并向左拖动 | 播放向左拖行动画,同时移动桌宠 |
|
|
54
54
|
| 鼠标移开或拖动结束 | 恢复默认待机动画 |
|
|
55
|
-
| 右键桌宠 |
|
|
55
|
+
| 右键桌宠 | 切换已有桌宠、刷新用量、检查更新或退出 |
|
|
56
56
|
|
|
57
57
|
右键菜单中的“切换桌宠”会动态扫描 `~/.petdex/pets/`、`~/.codex/pets/` 以及包内素材。
|
|
58
58
|
外部桌宠使用目录名作为菜单名称,内置桌宠使用素材文件名(不显示扩展名)。选择后立即
|
|
@@ -63,7 +63,8 @@ Claude Code Hook 会合并到 `~/.claude/settings.json`,现有的 `env`、`mod
|
|
|
63
63
|
`~/.codex/pets/<name>/` 查找同名桌宠的 `spritesheet.webp` 或 `spritesheet.png`。
|
|
64
64
|
外部素材遵循 Codex 桌面端的 `spriteVersionNumber` 协议:字段缺失或为 `1` 时按
|
|
65
65
|
`1536×1872`、`8×9` 网格解析;为 `2` 时按 `1536×2288`、`8×11` 网格解析。
|
|
66
|
-
v2
|
|
66
|
+
v2 新增的最后两行用于十六向鼠标跟随;只在桌宠空闲时启用,拖动、悬停互动和
|
|
67
|
+
Agent 状态动画会优先播放。
|
|
67
68
|
|
|
68
69
|
交互区域存在重叠时,按“口袋、头部、脚部、身体左右侧”的优先级识别。拖动超过
|
|
69
70
|
3 像素后才进入拖动状态,避免普通点击被误判为拖动。
|
|
@@ -131,6 +132,11 @@ npm 的安装后脚本会自动构建原生应用、安装两套 Agent Hooks,
|
|
|
131
132
|
同时会将标准 macOS 应用安装到 `~/Applications/CC Pets.app`,可从 Finder 或 Launchpad 启动,
|
|
132
133
|
不需要管理员权限。npm 升级时会同步更新该应用。
|
|
133
134
|
|
|
135
|
+
安装完成后也可以右键桌宠并选择“检查更新…”。应用会查询 npm Registry,发现新版本后请求确认,
|
|
136
|
+
随后在后台复用首次安装时记录的 Node.js/npm 完成升级;用户无需打开终端。升级会同步更新原生应用、
|
|
137
|
+
Codex/Claude Hooks 和 shell 集成,并自动重启正在运行的桌宠。自动更新不会请求管理员权限;如果原来的
|
|
138
|
+
全局 npm 目录不可写或 Node.js/npm 已被移动,应用会保留旧版本并显示更新日志和手动升级命令。
|
|
139
|
+
|
|
134
140
|
### 从旧版升级
|
|
135
141
|
|
|
136
142
|
从使用 `codex-pet` 命令的旧版升级时,直接安装新版即可:
|
package/Sources/CCPets/main.m
CHANGED
|
@@ -24,6 +24,38 @@ static NSString *ClaudeUsagePath(void) {
|
|
|
24
24
|
return [PetStateDirectory() stringByAppendingPathComponent:name];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
static NSArray<NSNumber *> *StableVersionComponents(NSString *version) {
|
|
28
|
+
if (![version isKindOfClass:NSString.class]) return nil;
|
|
29
|
+
NSArray<NSString *> *parts = [version componentsSeparatedByString:@"."];
|
|
30
|
+
if (parts.count != 3) return nil;
|
|
31
|
+
NSCharacterSet *nonDigits = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"].invertedSet;
|
|
32
|
+
NSMutableArray<NSNumber *> *components = [NSMutableArray arrayWithCapacity:3];
|
|
33
|
+
for (NSString *part in parts) {
|
|
34
|
+
if (part.length == 0 || part.length > 9 ||
|
|
35
|
+
(part.length > 1 && [part hasPrefix:@"0"]) ||
|
|
36
|
+
[part rangeOfCharacterFromSet:nonDigits].location != NSNotFound) return nil;
|
|
37
|
+
[components addObject:@(part.longLongValue)];
|
|
38
|
+
}
|
|
39
|
+
return components;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static NSComparisonResult CompareStableVersions(NSString *left, NSString *right, BOOL *valid) {
|
|
43
|
+
NSArray<NSNumber *> *leftParts = StableVersionComponents(left);
|
|
44
|
+
NSArray<NSNumber *> *rightParts = StableVersionComponents(right);
|
|
45
|
+
if (!leftParts || !rightParts) {
|
|
46
|
+
if (valid) *valid = NO;
|
|
47
|
+
return NSOrderedSame;
|
|
48
|
+
}
|
|
49
|
+
if (valid) *valid = YES;
|
|
50
|
+
for (NSUInteger index = 0; index < 3; index++) {
|
|
51
|
+
long long leftValue = leftParts[index].longLongValue;
|
|
52
|
+
long long rightValue = rightParts[index].longLongValue;
|
|
53
|
+
if (leftValue < rightValue) return NSOrderedAscending;
|
|
54
|
+
if (leftValue > rightValue) return NSOrderedDescending;
|
|
55
|
+
}
|
|
56
|
+
return NSOrderedSame;
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
static int RecordClaudeUsage(void) {
|
|
28
60
|
NSData *input = [NSFileHandle.fileHandleWithStandardInput readDataToEndOfFile];
|
|
29
61
|
NSDictionary *payload = input.length > 0 ? [NSJSONSerialization JSONObjectWithData:input options:0 error:nil] : nil;
|
|
@@ -268,6 +300,7 @@ static NSDictionary *LatestUsage(void) {
|
|
|
268
300
|
@interface PetView : NSView
|
|
269
301
|
@property NSImage *sheet;
|
|
270
302
|
@property NSInteger spriteRowCount;
|
|
303
|
+
@property NSInteger lookDirection;
|
|
271
304
|
@property NSInteger frameIndex;
|
|
272
305
|
@property NSInteger rowIndex;
|
|
273
306
|
@property NSArray<NSNumber *> *frames;
|
|
@@ -278,6 +311,8 @@ static NSDictionary *LatestUsage(void) {
|
|
|
278
311
|
@property NSPoint dragStartMouse;
|
|
279
312
|
@property NSPoint dragStartWindowOrigin;
|
|
280
313
|
@property NSString *currentPetID;
|
|
314
|
+
@property id globalMouseMonitor;
|
|
315
|
+
@property id localMouseMonitor;
|
|
281
316
|
@property(copy) void (^pocketHoverChanged)(BOOL hovering);
|
|
282
317
|
@property(copy) void (^positionChanged)(void);
|
|
283
318
|
@property(copy) void (^dragStateChanged)(BOOL dragging);
|
|
@@ -290,6 +325,7 @@ static NSDictionary *LatestUsage(void) {
|
|
|
290
325
|
if ((self = [super initWithFrame:frame])) {
|
|
291
326
|
_sheet = sheet;
|
|
292
327
|
_spriteRowCount = rowCount;
|
|
328
|
+
_lookDirection = -1;
|
|
293
329
|
_frames = @[@0, @1, @2, @3, @4, @5];
|
|
294
330
|
[NSTimer scheduledTimerWithTimeInterval:0.22 target:self
|
|
295
331
|
selector:@selector(nextFrame:) userInfo:nil repeats:YES];
|
|
@@ -298,9 +334,63 @@ static NSDictionary *LatestUsage(void) {
|
|
|
298
334
|
NSTrackingActiveAlways | NSTrackingInVisibleRect
|
|
299
335
|
owner:self userInfo:nil];
|
|
300
336
|
[self addTrackingArea:tracking];
|
|
337
|
+
[self updateLookTracking];
|
|
301
338
|
}
|
|
302
339
|
return self;
|
|
303
340
|
}
|
|
341
|
+
- (void)dealloc {
|
|
342
|
+
[self stopLookTracking];
|
|
343
|
+
}
|
|
344
|
+
- (void)startLookTracking {
|
|
345
|
+
if (self.spriteRowCount != 11 || self.globalMouseMonitor || self.localMouseMonitor) return;
|
|
346
|
+
__weak PetView *weakSelf = self;
|
|
347
|
+
self.globalMouseMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
|
|
348
|
+
handler:^(NSEvent *event) {
|
|
349
|
+
[weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
|
|
350
|
+
}];
|
|
351
|
+
self.localMouseMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskMouseMoved
|
|
352
|
+
handler:^NSEvent *(NSEvent *event) {
|
|
353
|
+
[weakSelf updateLookForScreenPoint:NSEvent.mouseLocation];
|
|
354
|
+
return event;
|
|
355
|
+
}];
|
|
356
|
+
}
|
|
357
|
+
- (void)stopLookTracking {
|
|
358
|
+
if (self.globalMouseMonitor) {
|
|
359
|
+
[NSEvent removeMonitor:self.globalMouseMonitor];
|
|
360
|
+
self.globalMouseMonitor = nil;
|
|
361
|
+
}
|
|
362
|
+
if (self.localMouseMonitor) {
|
|
363
|
+
[NSEvent removeMonitor:self.localMouseMonitor];
|
|
364
|
+
self.localMouseMonitor = nil;
|
|
365
|
+
}
|
|
366
|
+
self.lookDirection = -1;
|
|
367
|
+
}
|
|
368
|
+
- (void)updateLookTracking {
|
|
369
|
+
if (self.spriteRowCount == 11) [self startLookTracking];
|
|
370
|
+
else [self stopLookTracking];
|
|
371
|
+
}
|
|
372
|
+
- (void)updateLookForScreenPoint:(NSPoint)screenPoint {
|
|
373
|
+
if (self.spriteRowCount != 11 || self.draggingPet || self.oneShot || self.agentActive || !self.window) return;
|
|
374
|
+
|
|
375
|
+
NSPoint windowPoint = [self.window convertPointFromScreen:screenPoint];
|
|
376
|
+
NSPoint localPoint = [self convertPoint:windowPoint fromView:nil];
|
|
377
|
+
if (NSPointInRect(localPoint, self.bounds)) return;
|
|
378
|
+
|
|
379
|
+
NSPoint center = [self convertPoint:NSMakePoint(NSMidX(self.bounds), NSMidY(self.bounds)) toView:nil];
|
|
380
|
+
center = [self.window convertPointToScreen:center];
|
|
381
|
+
CGFloat dx = screenPoint.x - center.x;
|
|
382
|
+
CGFloat dy = screenPoint.y - center.y;
|
|
383
|
+
if (hypot(dx, dy) <= 1.0) return;
|
|
384
|
+
|
|
385
|
+
CGFloat degrees = fmod(atan2(dx, dy) * 180.0 / M_PI + 360.0, 360.0);
|
|
386
|
+
NSInteger direction = ((NSInteger)llround(degrees / 22.5)) % 16;
|
|
387
|
+
if (direction == self.lookDirection) return;
|
|
388
|
+
|
|
389
|
+
self.lookDirection = direction;
|
|
390
|
+
self.rowIndex = 9 + direction / 8;
|
|
391
|
+
self.frameIndex = direction % 8;
|
|
392
|
+
self.needsDisplay = YES;
|
|
393
|
+
}
|
|
304
394
|
- (NSArray<NSNumber *> *)framesFromZeroThrough:(NSInteger)last {
|
|
305
395
|
NSMutableArray<NSNumber *> *result = [NSMutableArray arrayWithCapacity:last + 1];
|
|
306
396
|
for (NSInteger index = 0; index <= last; index++) [result addObject:@(index)];
|
|
@@ -308,6 +398,7 @@ static NSDictionary *LatestUsage(void) {
|
|
|
308
398
|
}
|
|
309
399
|
- (void)playRow:(NSInteger)row throughFrame:(NSInteger)last oneShot:(BOOL)oneShot {
|
|
310
400
|
if (!oneShot && !self.oneShot && self.rowIndex == row) return;
|
|
401
|
+
self.lookDirection = -1;
|
|
311
402
|
self.rowIndex = row;
|
|
312
403
|
self.frames = [self framesFromZeroThrough:last];
|
|
313
404
|
self.sequenceIndex = 0;
|
|
@@ -365,6 +456,7 @@ static NSDictionary *LatestUsage(void) {
|
|
|
365
456
|
[self playRow:0 throughFrame:5 oneShot:NO];
|
|
366
457
|
}
|
|
367
458
|
- (void)nextFrame:(NSTimer *)timer {
|
|
459
|
+
if (self.lookDirection >= 0) return;
|
|
368
460
|
self.sequenceIndex += 1;
|
|
369
461
|
if (self.sequenceIndex >= self.frames.count) {
|
|
370
462
|
if (self.oneShot) {
|
|
@@ -469,12 +561,14 @@ static NSDictionary *LatestUsage(void) {
|
|
|
469
561
|
- (void)applySheet:(NSImage *)sheet petID:(NSString *)petID rowCount:(NSInteger)rowCount {
|
|
470
562
|
self.sheet = sheet;
|
|
471
563
|
self.spriteRowCount = rowCount;
|
|
564
|
+
self.lookDirection = -1;
|
|
472
565
|
self.currentPetID = petID;
|
|
473
566
|
self.rowIndex = 0;
|
|
474
567
|
self.frameIndex = 0;
|
|
475
568
|
self.sequenceIndex = 0;
|
|
476
569
|
self.frames = [self framesFromZeroThrough:5];
|
|
477
570
|
self.oneShot = NO;
|
|
571
|
+
[self updateLookTracking];
|
|
478
572
|
self.needsDisplay = YES;
|
|
479
573
|
}
|
|
480
574
|
- (void)selectPet:(NSMenuItem *)sender {
|
|
@@ -498,6 +592,8 @@ static NSDictionary *LatestUsage(void) {
|
|
|
498
592
|
[menu addItem:NSMenuItem.separatorItem];
|
|
499
593
|
NSMenuItem *refreshItem = [menu addItemWithTitle:@"刷新用量" action:@selector(refreshUsage:) keyEquivalent:@"r"];
|
|
500
594
|
refreshItem.target = NSApp.delegate;
|
|
595
|
+
NSMenuItem *updateItem = [menu addItemWithTitle:@"检查更新…" action:@selector(checkForUpdates:) keyEquivalent:@""];
|
|
596
|
+
updateItem.target = NSApp.delegate;
|
|
501
597
|
[menu addItem:NSMenuItem.separatorItem];
|
|
502
598
|
NSMenuItem *quitItem = [menu addItemWithTitle:@"退出桌宠" action:@selector(terminate:) keyEquivalent:@"q"];
|
|
503
599
|
quitItem.target = NSApp;
|
|
@@ -856,6 +952,9 @@ static int RenderQuotaDashboard(NSString *path) {
|
|
|
856
952
|
@property NSMutableData *agentEventPartialLine;
|
|
857
953
|
@property dispatch_source_t agentEventSource;
|
|
858
954
|
@property BOOL agentEventReaderInitialized;
|
|
955
|
+
@property BOOL checkingForUpdate;
|
|
956
|
+
@property BOOL updating;
|
|
957
|
+
@property NSTask *updateTask;
|
|
859
958
|
- (void)showQuotaDashboard;
|
|
860
959
|
- (void)positionQuotaDashboard;
|
|
861
960
|
- (void)scheduleQuotaDashboardHide;
|
|
@@ -863,6 +962,159 @@ static int RenderQuotaDashboard(NSString *path) {
|
|
|
863
962
|
@end
|
|
864
963
|
|
|
865
964
|
@implementation AppDelegate
|
|
965
|
+
- (NSString *)applicationSupportDirectory {
|
|
966
|
+
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_APPLICATION_SUPPORT_DIR"];
|
|
967
|
+
if (override.length > 0) return override.stringByStandardizingPath;
|
|
968
|
+
NSString *root = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject;
|
|
969
|
+
return [root stringByAppendingPathComponent:@"CC Pets"];
|
|
970
|
+
}
|
|
971
|
+
- (void)showUpdateAlertWithTitle:(NSString *)title message:(NSString *)message {
|
|
972
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
973
|
+
NSAlert *alert = [NSAlert new];
|
|
974
|
+
alert.messageText = title;
|
|
975
|
+
alert.informativeText = message;
|
|
976
|
+
[alert addButtonWithTitle:@"好"];
|
|
977
|
+
[alert runModal];
|
|
978
|
+
}
|
|
979
|
+
- (NSDictionary *)updaterConfiguration {
|
|
980
|
+
NSString *path = [[self applicationSupportDirectory] stringByAppendingPathComponent:@"updater.json"];
|
|
981
|
+
NSData *data = [NSData dataWithContentsOfFile:path];
|
|
982
|
+
NSDictionary *configuration = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
983
|
+
return [configuration isKindOfClass:NSDictionary.class] ? configuration : nil;
|
|
984
|
+
}
|
|
985
|
+
- (void)startUpdateToVersion:(NSString *)version {
|
|
986
|
+
if (self.updating) return;
|
|
987
|
+
NSDictionary *configuration = [self updaterConfiguration];
|
|
988
|
+
NSString *nodePath = [configuration[@"nodePath"] isKindOfClass:NSString.class]
|
|
989
|
+
? [configuration[@"nodePath"] stringByStandardizingPath] : nil;
|
|
990
|
+
NSString *npmCliPath = [configuration[@"npmCliPath"] isKindOfClass:NSString.class]
|
|
991
|
+
? [configuration[@"npmCliPath"] stringByStandardizingPath] : nil;
|
|
992
|
+
BOOL nodeExecutable = nodePath.isAbsolutePath &&
|
|
993
|
+
[NSFileManager.defaultManager isExecutableFileAtPath:nodePath];
|
|
994
|
+
BOOL npmCliExists = npmCliPath.isAbsolutePath &&
|
|
995
|
+
[NSFileManager.defaultManager isReadableFileAtPath:npmCliPath];
|
|
996
|
+
if (!nodeExecutable || !npmCliExists) {
|
|
997
|
+
[self showUpdateAlertWithTitle:@"无法自动更新"
|
|
998
|
+
message:@"没有找到安装 CC Pets 时使用的 Node.js/npm。请先手动执行一次:\n\n"
|
|
999
|
+
"npm install -g cc-pets@latest --allow-scripts=cc-pets --prefer-online"];
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
NSString *supportDirectory = [self applicationSupportDirectory];
|
|
1004
|
+
NSError *directoryError = nil;
|
|
1005
|
+
[NSFileManager.defaultManager createDirectoryAtPath:supportDirectory
|
|
1006
|
+
withIntermediateDirectories:YES attributes:nil error:&directoryError];
|
|
1007
|
+
if (directoryError) {
|
|
1008
|
+
[self showUpdateAlertWithTitle:@"无法自动更新" message:directoryError.localizedDescription];
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
NSString *logPath = [supportDirectory stringByAppendingPathComponent:@"update.log"];
|
|
1012
|
+
[NSFileManager.defaultManager createFileAtPath:logPath contents:nil attributes:@{
|
|
1013
|
+
NSFilePosixPermissions: @(S_IRUSR | S_IWUSR)
|
|
1014
|
+
}];
|
|
1015
|
+
NSFileHandle *logHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
|
|
1016
|
+
if (!logHandle) {
|
|
1017
|
+
[self showUpdateAlertWithTitle:@"无法自动更新" message:@"无法创建更新日志。"];
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
NSTask *task = [NSTask new];
|
|
1022
|
+
task.executableURL = [NSURL fileURLWithPath:nodePath];
|
|
1023
|
+
task.arguments = @[
|
|
1024
|
+
npmCliPath,
|
|
1025
|
+
@"install",
|
|
1026
|
+
@"--global",
|
|
1027
|
+
[@"cc-pets@" stringByAppendingString:version],
|
|
1028
|
+
@"--allow-scripts=cc-pets",
|
|
1029
|
+
@"--prefer-online"
|
|
1030
|
+
];
|
|
1031
|
+
task.currentDirectoryURL = [NSURL fileURLWithPath:NSHomeDirectory()];
|
|
1032
|
+
task.standardOutput = logHandle;
|
|
1033
|
+
task.standardError = logHandle;
|
|
1034
|
+
self.updating = YES;
|
|
1035
|
+
self.updateTask = task;
|
|
1036
|
+
__weak typeof(self) weakSelf = self;
|
|
1037
|
+
task.terminationHandler = ^(NSTask *finishedTask) {
|
|
1038
|
+
[logHandle closeFile];
|
|
1039
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
1040
|
+
typeof(self) strongSelf = weakSelf;
|
|
1041
|
+
if (!strongSelf) return;
|
|
1042
|
+
strongSelf.updating = NO;
|
|
1043
|
+
strongSelf.updateTask = nil;
|
|
1044
|
+
if (finishedTask.terminationStatus == EXIT_SUCCESS) {
|
|
1045
|
+
[strongSelf showUpdateAlertWithTitle:@"更新完成"
|
|
1046
|
+
message:@"CC Pets 已更新。如果桌宠没有自动重启,请重新启动一次。"];
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
1050
|
+
NSAlert *alert = [NSAlert new];
|
|
1051
|
+
alert.messageText = @"更新失败";
|
|
1052
|
+
alert.informativeText = [NSString stringWithFormat:
|
|
1053
|
+
@"旧版本仍可继续使用。错误详情已写入:\n%@", logPath];
|
|
1054
|
+
[alert addButtonWithTitle:@"打开日志"];
|
|
1055
|
+
[alert addButtonWithTitle:@"关闭"];
|
|
1056
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
1057
|
+
[NSWorkspace.sharedWorkspace openURL:[NSURL fileURLWithPath:logPath]];
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
};
|
|
1061
|
+
NSError *launchError = nil;
|
|
1062
|
+
if (![task launchAndReturnError:&launchError]) {
|
|
1063
|
+
self.updating = NO;
|
|
1064
|
+
self.updateTask = nil;
|
|
1065
|
+
[logHandle closeFile];
|
|
1066
|
+
[self showUpdateAlertWithTitle:@"无法启动更新" message:launchError.localizedDescription];
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
[self showUpdateAlertWithTitle:@"正在更新"
|
|
1070
|
+
message:[NSString stringWithFormat:@"正在下载并安装 CC Pets %@。完成后桌宠会自动重启。", version]];
|
|
1071
|
+
}
|
|
1072
|
+
- (void)checkForUpdates:(id)sender {
|
|
1073
|
+
if (self.checkingForUpdate || self.updating) return;
|
|
1074
|
+
self.checkingForUpdate = YES;
|
|
1075
|
+
NSURL *url = [NSURL URLWithString:@"https://registry.npmjs.org/cc-pets/latest"];
|
|
1076
|
+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
|
|
1077
|
+
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
|
|
1078
|
+
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
|
1079
|
+
__weak typeof(self) weakSelf = self;
|
|
1080
|
+
NSURLSessionDataTask *task = [NSURLSession.sharedSession dataTaskWithRequest:request
|
|
1081
|
+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
1082
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
1083
|
+
typeof(self) strongSelf = weakSelf;
|
|
1084
|
+
if (!strongSelf) return;
|
|
1085
|
+
strongSelf.checkingForUpdate = NO;
|
|
1086
|
+
NSHTTPURLResponse *httpResponse = [response isKindOfClass:NSHTTPURLResponse.class]
|
|
1087
|
+
? (NSHTTPURLResponse *)response : nil;
|
|
1088
|
+
NSDictionary *metadata = data
|
|
1089
|
+
? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
1090
|
+
NSString *latestVersion = [metadata[@"version"] isKindOfClass:NSString.class]
|
|
1091
|
+
? metadata[@"version"] : nil;
|
|
1092
|
+
BOOL valid = NO;
|
|
1093
|
+
NSComparisonResult comparison = CompareStableVersions(@CC_PETS_VERSION, latestVersion, &valid);
|
|
1094
|
+
if (error || httpResponse.statusCode != 200 || !valid) {
|
|
1095
|
+
NSString *message = error.localizedDescription ?: @"npm Registry 返回了无效的版本信息,请稍后重试。";
|
|
1096
|
+
[strongSelf showUpdateAlertWithTitle:@"检查更新失败" message:message];
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
if (comparison != NSOrderedAscending) {
|
|
1100
|
+
[strongSelf showUpdateAlertWithTitle:@"已是最新版本"
|
|
1101
|
+
message:[NSString stringWithFormat:@"当前版本:%@", @CC_PETS_VERSION]];
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
1104
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
1105
|
+
NSAlert *alert = [NSAlert new];
|
|
1106
|
+
alert.messageText = @"发现新版本";
|
|
1107
|
+
alert.informativeText = [NSString stringWithFormat:
|
|
1108
|
+
@"当前版本:%@\n最新版本:%@\n\n是否立即更新?", @CC_PETS_VERSION, latestVersion];
|
|
1109
|
+
[alert addButtonWithTitle:@"立即更新"];
|
|
1110
|
+
[alert addButtonWithTitle:@"稍后"];
|
|
1111
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
1112
|
+
[strongSelf startUpdateToVersion:latestVersion];
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
}];
|
|
1116
|
+
[task resume];
|
|
1117
|
+
}
|
|
866
1118
|
- (NSDictionary *)petManifestInDirectory:(NSString *)directory {
|
|
867
1119
|
NSString *jsonPath = [directory stringByAppendingPathComponent:@"pet.json"];
|
|
868
1120
|
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
|
|
@@ -1310,6 +1562,16 @@ int main(int argc, const char *argv[]) {
|
|
|
1310
1562
|
printf("cc-pets %s\n", CC_PETS_VERSION);
|
|
1311
1563
|
return EXIT_SUCCESS;
|
|
1312
1564
|
}
|
|
1565
|
+
if (argc > 3 && strcmp(argv[1], "--compare-versions") == 0) {
|
|
1566
|
+
BOOL valid = NO;
|
|
1567
|
+
NSComparisonResult comparison = CompareStableVersions(
|
|
1568
|
+
[NSString stringWithUTF8String:argv[2]],
|
|
1569
|
+
[NSString stringWithUTF8String:argv[3]],
|
|
1570
|
+
&valid);
|
|
1571
|
+
if (!valid) return 2;
|
|
1572
|
+
printf("%ld\n", (long)comparison);
|
|
1573
|
+
return EXIT_SUCCESS;
|
|
1574
|
+
}
|
|
1313
1575
|
if (argc > 1 && strcmp(argv[1], "--hook") == 0) return RecordHookEvent();
|
|
1314
1576
|
if (argc > 1 && strcmp(argv[1], "--claude-usage") == 0) return RecordClaudeUsage();
|
|
1315
1577
|
if (argc > 2 && strcmp(argv[1], "--render-dashboard") == 0) {
|
package/package.json
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
const nodeCandidate = process.env.npm_node_execpath || process.execPath;
|
|
8
|
+
const npmCandidate = process.env.npm_execpath;
|
|
9
|
+
|
|
10
|
+
if (!nodeCandidate || !npmCandidate) {
|
|
11
|
+
console.warn("未记录自动更新配置:npm 没有提供 node/npm-cli 路径。");
|
|
12
|
+
process.exit(0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let nodePath;
|
|
16
|
+
let npmCliPath;
|
|
17
|
+
try {
|
|
18
|
+
nodePath = fs.realpathSync(nodeCandidate);
|
|
19
|
+
npmCliPath = fs.realpathSync(npmCandidate);
|
|
20
|
+
} catch {
|
|
21
|
+
console.warn("未记录自动更新配置:node/npm-cli 路径不存在。");
|
|
22
|
+
process.exit(0);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!path.isAbsolute(nodePath) || !path.isAbsolute(npmCliPath)) {
|
|
26
|
+
console.warn("未记录自动更新配置:node/npm-cli 路径不是绝对路径。");
|
|
27
|
+
process.exit(0);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const supportDirectory = process.env.CC_PETS_APPLICATION_SUPPORT_DIR ||
|
|
31
|
+
path.join(os.homedir(), "Library", "Application Support", "CC Pets");
|
|
32
|
+
const configPath = path.join(supportDirectory, "updater.json");
|
|
33
|
+
fs.mkdirSync(supportDirectory, {recursive: true, mode: 0o700});
|
|
34
|
+
fs.writeFileSync(configPath, `${JSON.stringify({
|
|
35
|
+
schemaVersion: 1,
|
|
36
|
+
nodePath,
|
|
37
|
+
npmCliPath
|
|
38
|
+
}, null, 2)}\n`, {mode: 0o600});
|
|
39
|
+
fs.chmodSync(configPath, 0o600);
|
|
@@ -18,6 +18,7 @@ node "${PROJECT_DIR}/scripts/install-codex-hooks.mjs" "${PROJECT_DIR}/.build/rel
|
|
|
18
18
|
node "${PROJECT_DIR}/scripts/install-claude-hooks.mjs" \
|
|
19
19
|
"${PROJECT_DIR}/.build/release/cc-pets" \
|
|
20
20
|
"${PROJECT_DIR}/bin/claude-statusline-with-pet"
|
|
21
|
+
node "${PROJECT_DIR}/scripts/configure-updater.mjs"
|
|
21
22
|
|
|
22
23
|
PET_APP="${PROJECT_DIR}/.build/release/CC Pets.app"
|
|
23
24
|
PET_APP_EXECUTABLE="${PET_APP}/Contents/MacOS/cc-pets"
|