cc-pets 2.4.0 → 2.5.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 +46 -9
- package/CC_BRIDGE.zh-CN.md +29 -6
- package/CHANGELOG.md +55 -0
- package/CHANGELOG.zh-CN.md +56 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +69 -16
- package/README.zh-CN.md +54 -19
- package/Resources/zh-Hans.lproj/Localizable.strings +35 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +109 -14
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +8 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +13 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +5 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
- package/Sources/CCPets/Bridge/CCPetsBridge.m +34 -12
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +100 -33
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +317 -89
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +210 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +28 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +222 -35
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +6 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +54 -5
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +9 -1
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +10 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +528 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +95 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +4 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +16 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
- package/package.json +1 -1
|
@@ -93,10 +93,11 @@ static NSString *USAGE(void) {
|
|
|
93
93
|
|
|
94
94
|
#pragma mark - 参数
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
// 只在前 limit 个参数里找:send 的正文可以出现任意文字,包括 --from。
|
|
97
|
+
static NSString *ParseFlagWithin(NSMutableArray<NSString *> *arguments, NSString *name, NSUInteger limit) {
|
|
97
98
|
NSString *exact = [@"--" stringByAppendingString:name];
|
|
98
99
|
NSString *prefix = [exact stringByAppendingString:@"="];
|
|
99
|
-
for (NSUInteger index = 0; index < arguments.count; index++) {
|
|
100
|
+
for (NSUInteger index = 0; index < MIN(limit, arguments.count); index++) {
|
|
100
101
|
NSString *argument = arguments[index];
|
|
101
102
|
if (![argument isEqualToString:exact] && ![argument hasPrefix:prefix]) continue;
|
|
102
103
|
[arguments removeObjectAtIndex:index];
|
|
@@ -106,11 +107,15 @@ static NSString *ParseFlag(NSMutableArray<NSString *> *arguments, NSString *name
|
|
|
106
107
|
[arguments removeObjectAtIndex:index];
|
|
107
108
|
return value;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
+
Fail(LF(L(@"{flag} needs a value."), @{@"flag": exact}));
|
|
110
111
|
}
|
|
111
112
|
return nil;
|
|
112
113
|
}
|
|
113
114
|
|
|
115
|
+
static NSString *ParseFlag(NSMutableArray<NSString *> *arguments, NSString *name) {
|
|
116
|
+
return ParseFlagWithin(arguments, name, NSUIntegerMax);
|
|
117
|
+
}
|
|
118
|
+
|
|
114
119
|
static NSDictionary<NSString *, NSString *> *ReadOptionFlags(NSMutableArray<NSString *> *arguments) {
|
|
115
120
|
NSMutableDictionary<NSString *, NSString *> *flags = [NSMutableDictionary dictionary];
|
|
116
121
|
NSDictionary<NSString *, NSString *> *names = @{@"approve": @"approve", @"codex-approve": @"codexApprove",
|
|
@@ -126,6 +131,8 @@ static NSDictionary *MergedOptions(NSDictionary *base, NSMutableArray<NSString *
|
|
|
126
131
|
NSString *error = nil;
|
|
127
132
|
NSDictionary *options = CCBridgeMergeOptionFlags(base, ReadOptionFlags(arguments), &error);
|
|
128
133
|
if (!options) Fail(error);
|
|
134
|
+
// 拼错的选项不能当成"没给":用户以为改了,其实什么都没变。
|
|
135
|
+
if (arguments.count > 0) Fail(LF(L(@"Unknown option: {option}"), @{@"option": arguments.firstObject}));
|
|
129
136
|
return options;
|
|
130
137
|
}
|
|
131
138
|
|
|
@@ -136,15 +143,15 @@ static void PrintSessions(void) {
|
|
|
136
143
|
return;
|
|
137
144
|
}
|
|
138
145
|
for (NSDictionary *session in sessions) {
|
|
139
|
-
Print([NSString stringWithFormat:@"%@ %@ %@ pid=%@ %@ %@ %@=%lu",
|
|
146
|
+
Print([NSString stringWithFormat:@"%@ %@ %@ pid=%@ %@ %@ %@=%lu", CCBridgeDescribeSessionForDisplay(session), session[@"provider"],
|
|
140
147
|
StringValue(session[@"status"]) ?: @"-", session[@"pid"] ?: @"-", StringValue(session[@"tty"]) ?: @"-",
|
|
141
148
|
StringValue(session[@"cwd"]) ?: @"-", L(@"pending"), (unsigned long)CCBridgePendingCount(session[@"session"])]);
|
|
142
149
|
}
|
|
143
150
|
}
|
|
144
151
|
|
|
145
|
-
static NSDictionary *SelfOrFrom(NSMutableArray<NSString *> *arguments) {
|
|
152
|
+
static NSDictionary *SelfOrFrom(NSMutableArray<NSString *> *arguments, NSUInteger flagLimit) {
|
|
146
153
|
if (!CCBridgeEnabled()) Fail(L(@"CC Bridge is off. Run cc-pets bridge enable first."));
|
|
147
|
-
NSString *from =
|
|
154
|
+
NSString *from = ParseFlagWithin(arguments, @"from", flagLimit);
|
|
148
155
|
if (from.length > 0) {
|
|
149
156
|
NSDictionary *resolved = CCBridgeResolveTarget(from, CCBridgeLiveSessions());
|
|
150
157
|
if (resolved[@"error"]) Fail(resolved[@"error"]);
|
|
@@ -157,14 +164,23 @@ static NSDictionary *SelfOrFrom(NSMutableArray<NSString *> *arguments) {
|
|
|
157
164
|
|
|
158
165
|
#pragma mark - hook
|
|
159
166
|
|
|
167
|
+
static NSString *SessionIdentityContext(NSString *sessionId) {
|
|
168
|
+
return LF(L(@"[cc-bridge identity] Your current session_id is {session_id}. Pass this exact value as session_id on every CC Bridge tool call. Do not use a recipient's ID or an ID from a cross-session message."),
|
|
169
|
+
@{@"session_id": sessionId});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static NSDictionary *HookContext(NSString *eventName, NSString *context) {
|
|
173
|
+
return @{@"hookSpecificOutput": @{@"hookEventName": eventName, @"additionalContext": context}};
|
|
174
|
+
}
|
|
175
|
+
|
|
160
176
|
static NSDictionary *InjectPending(NSString *sessionId, NSString *eventName) {
|
|
161
177
|
NSArray<NSDictionary *> *messages = ClaimForOutput(sessionId, @"prompt-inject");
|
|
162
|
-
|
|
163
|
-
NSMutableArray<NSString *> *envelopes = [NSMutableArray
|
|
178
|
+
// 每次用户输入都重新提供身份,覆盖 resume、上下文压缩和 MCP 重新连接。
|
|
179
|
+
NSMutableArray<NSString *> *envelopes = [NSMutableArray arrayWithObject:SessionIdentityContext(sessionId)];
|
|
164
180
|
for (NSDictionary *message in messages) {
|
|
165
181
|
[envelopes addObject:CCBridgeFormatEnvelope(message)];
|
|
166
182
|
}
|
|
167
|
-
return
|
|
183
|
+
return HookContext(eventName, [envelopes componentsJoinedByString:@"\n\n"]);
|
|
168
184
|
}
|
|
169
185
|
|
|
170
186
|
// 首次拦截、重试放行:本会话第一次编辑某处他人预留的文件时返回 deny 并说明原因,同时记下"已提醒";
|
|
@@ -206,15 +222,25 @@ static NSDictionary *RunHook(NSString *input) {
|
|
|
206
222
|
if (event.length == 0 || !sessionId) return nil;
|
|
207
223
|
|
|
208
224
|
if ([event isEqualToString:@"SessionEnd"]) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
225
|
+
// 各步互不依赖:一步等锁超时,其余的清理也要做完。
|
|
226
|
+
for (void (^step)(void) in @[
|
|
227
|
+
^{ CCBridgeRemoveSession(sessionId); },
|
|
228
|
+
^{ CCBridgeAbandonInbox(sessionId); },
|
|
229
|
+
^{ CCBridgeReleaseSessionReservations(sessionId); },
|
|
230
|
+
^{ CCBridgeClearIdleSubscriptions(sessionId); },
|
|
231
|
+
^{ CCBridgeDropWatcher(sessionId); }
|
|
232
|
+
]) {
|
|
233
|
+
@try { step(); } @catch (NSException *exception) {}
|
|
234
|
+
}
|
|
212
235
|
return nil;
|
|
213
236
|
}
|
|
214
237
|
// 编辑前检查文件预留。不更新注册表——这个 hook 每次编辑都会跑,保持最轻。
|
|
215
238
|
if ([event isEqualToString:@"PreToolUse"]) {
|
|
216
239
|
return [CCBridgeOptions()[@"editGuard"] boolValue] ? CheckReservations(payload, sessionId) : nil;
|
|
217
240
|
}
|
|
241
|
+
// 没有持久化转录的 Codex 临时任务不能作为 queue 的收件会话,但仍需检查文件预留。
|
|
242
|
+
// 缺字段的旧版 hook 保持兼容;SessionEnd 仍先走上面的清理。
|
|
243
|
+
if ([provider isEqualToString:@"Codex"] && payload[@"transcript_path"] == NSNull.null) return nil;
|
|
218
244
|
|
|
219
245
|
NSString *status = @{@"SessionStart": @"idle", @"UserPromptSubmit": @"busy", @"Stop": @"idle"}[event];
|
|
220
246
|
NSDictionary *known = CCBridgeReadSession(sessionId);
|
|
@@ -230,8 +256,13 @@ static NSDictionary *RunHook(NSString *input) {
|
|
|
230
256
|
CCPetsEnvironmentValue(@"CC_PETS_PROFILE_NAME"), @[]);
|
|
231
257
|
|
|
232
258
|
if ([event isEqualToString:@"SessionStart"]) {
|
|
233
|
-
|
|
234
|
-
|
|
259
|
+
// 清理失败不能影响身份注入。
|
|
260
|
+
@try {
|
|
261
|
+
CCBridgeSweepMessages();
|
|
262
|
+
CCBridgePruneReceipts();
|
|
263
|
+
} @catch (NSException *exception) {
|
|
264
|
+
}
|
|
265
|
+
return HookContext(event, SessionIdentityContext(sessionId));
|
|
235
266
|
}
|
|
236
267
|
if ([event isEqualToString:@"UserPromptSubmit"]) return InjectPending(sessionId, event);
|
|
237
268
|
// 空闲通知可能要调 codex queue(秒级),放到脱离的子进程里做,不让 Stop hook 拖慢对方回合的收尾。
|
|
@@ -259,7 +290,8 @@ static int HookCommand(void) {
|
|
|
259
290
|
#pragma mark - watch
|
|
260
291
|
// Claude 端的投递者:以 asyncRewake hook 运行,等信箱来消息后 exit 2 唤醒会话。实测:exit 2 时 stderr
|
|
261
292
|
// 作为系统提醒进入会话,空闲会被唤醒、忙碌则注入当前回合;hook 的 timeout 会被强制执行,所以 watcher
|
|
262
|
-
// 在超时前主动退出,下一次事件再重新挂上。每个会话只保留一个 watcher
|
|
293
|
+
// 在超时前主动退出,下一次事件再重新挂上。每个会话只保留一个 watcher(锁文件):新挂上的取代旧的,
|
|
294
|
+
// 并从自己启动时重新计时,否则活跃的会话也会在第一个 watcher 到期后断掉唤醒,直到下一轮结束。
|
|
263
295
|
|
|
264
296
|
static int WatchCommand(void) {
|
|
265
297
|
NSString *lock = nil;
|
|
@@ -270,8 +302,7 @@ static int WatchCommand(void) {
|
|
|
270
302
|
NSDictionary *payload = ParseJSON(ReadStandardInput());
|
|
271
303
|
NSString *sessionId = [payload isKindOfClass:NSDictionary.class] ? CCBridgeSafeSessionId(payload[@"session_id"]) : nil;
|
|
272
304
|
if (!sessionId) return 0;
|
|
273
|
-
|
|
274
|
-
if (!lock) return 0;
|
|
305
|
+
NSString *token = CCBridgeClaimWatcher(sessionId);
|
|
275
306
|
// 安装时 hook 的 timeout 是 3600 秒,这里留 5 分钟余量主动退出,保证锁由自己释放。
|
|
276
307
|
double seconds = CCPetsEnvironmentValue(@"CC_BRIDGE_WATCH_SECONDS").doubleValue;
|
|
277
308
|
if (!(seconds > 0)) seconds = 3300;
|
|
@@ -283,7 +314,16 @@ static int WatchCommand(void) {
|
|
|
283
314
|
if (!CCBridgeEnabled() || ![CCBridgeOptions()[@"wake"] boolValue]) return 0;
|
|
284
315
|
// 宿主会话已经没了(被 kill -9 等,没来得及清理子进程)就别再占着锁。
|
|
285
316
|
if (!CCBridgeProcessAlive(parent)) return 0;
|
|
286
|
-
|
|
317
|
+
if (!CCBridgeWatcherOwned(sessionId, token)) return 0;
|
|
318
|
+
// 旧 watcher 下一次轮询就会退出。升级时跑的旧版本不认登记,那就一直等到它到期。
|
|
319
|
+
if (!lock) lock = CCBridgeAcquireLock([@"watch-" stringByAppendingString:sessionId]);
|
|
320
|
+
if (!lock) {
|
|
321
|
+
usleep((useconds_t)(poll * 1000));
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
// 这里每秒走一次:信箱空着时只列目录,不去写删锁文件。
|
|
325
|
+
NSArray<NSDictionary *> *messages = CCBridgePendingCount(sessionId) > 0
|
|
326
|
+
? ClaimForOutput(sessionId, @"claude-rewake") : @[];
|
|
287
327
|
if (messages.count > 0) {
|
|
288
328
|
NSMutableArray<NSString *> *envelopes = [NSMutableArray array];
|
|
289
329
|
for (NSDictionary *message in messages) {
|
|
@@ -326,7 +366,10 @@ static NSString *AliasNote(void) {
|
|
|
326
366
|
}
|
|
327
367
|
|
|
328
368
|
static NSDictionary *Schema(NSDictionary *properties, NSArray<NSString *> *required) {
|
|
329
|
-
NSMutableDictionary *
|
|
369
|
+
NSMutableDictionary *withIdentity = [properties mutableCopy];
|
|
370
|
+
withIdentity[@"session_id"] = @{@"type": @"string", @"minLength": @1, @"maxLength": @128,
|
|
371
|
+
@"description": L(@"Your current session ID from the cc-bridge hook context or your own shell's CODEX_THREAD_ID / CLAUDE_CODE_SESSION_ID. Pass it on every call when available; required to identify one of several sessions sharing a host. Never use the recipient's ID or guess from the session list.")};
|
|
372
|
+
NSMutableDictionary *schema = [@{@"type": @"object", @"properties": withIdentity, @"additionalProperties": @NO} mutableCopy];
|
|
330
373
|
if (required) schema[@"required"] = required;
|
|
331
374
|
return schema;
|
|
332
375
|
}
|
|
@@ -355,7 +398,7 @@ static NSArray<NSDictionary *> *Tools(void) {
|
|
|
355
398
|
@"message": @{@"type": @"string",
|
|
356
399
|
@"description": L(@"Message body. Say what it's about in the first sentence; don't rely on @file references, since attachments aren't delivered.")},
|
|
357
400
|
@"notify_when_idle": @{@"type": @"boolean",
|
|
358
|
-
@"description": L(@"When true, this session gets a one-time notice when the recipient next finishes a turn and goes idle.")}
|
|
401
|
+
@"description": L(@"When true, this session gets a one-time notice when the recipient next finishes a turn and goes idle. That turn may be one already in progress, so the notice doesn't mean this message was handled.")}
|
|
359
402
|
}, @[@"to", @"message"]),
|
|
360
403
|
@"annotations": @{@"readOnlyHint": @NO, @"destructiveHint": @NO, @"title": L(@"Send cross-session message")}},
|
|
361
404
|
@{@"name": @"set_name",
|
|
@@ -415,8 +458,12 @@ static NSString *AgeText(id since) {
|
|
|
415
458
|
return LF(L(@"started {minutes} h ago"), @{@"minutes": @((long long)floor(minutes / 60 + 0.5))});
|
|
416
459
|
}
|
|
417
460
|
|
|
461
|
+
static NSString *IdentityNote(void) {
|
|
462
|
+
return L(@" Pass your current session_id on every CC Bridge tool call, using the ID from the cc-bridge hook context. If it is missing, read CODEX_THREAD_ID (Codex) or CLAUDE_CODE_SESSION_ID (Claude) in your own session's shell; if neither is available, refresh this session's bridge hooks. Never guess an ID from names, directories, activity or received messages, and never save one session's ID in shared MCP configuration.");
|
|
463
|
+
}
|
|
464
|
+
|
|
418
465
|
static NSString *UnknownSelf(void) {
|
|
419
|
-
return L(@"Current session identity is unavailable or ambiguous. Register this session through the bridge hooks and provide a valid session ID. A shared host PID cannot identify one of several sessions; no action was taken.");
|
|
466
|
+
return [L(@"Current session identity is unavailable or ambiguous. Register this session through the bridge hooks and provide a valid session ID. A shared host PID cannot identify one of several sessions; no action was taken.") stringByAppendingString:IdentityNote()];
|
|
420
467
|
}
|
|
421
468
|
|
|
422
469
|
static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
@@ -424,7 +471,14 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
424
471
|
return TextResult(L(@"cc-pets CC Bridge isn't enabled. The user can turn it on by running `cc-pets bridge enable` in a terminal."), YES);
|
|
425
472
|
}
|
|
426
473
|
if (![arguments isKindOfClass:NSDictionary.class]) arguments = @{};
|
|
427
|
-
|
|
474
|
+
NSString *identityFailure = nil;
|
|
475
|
+
NSDictionary *self = CCBridgeIdentifySelfForSession(arguments[@"session_id"], &identityFailure);
|
|
476
|
+
// 即使是列表工具,显式传错身份也要报错,不能伪装成一次成功的匿名查询。
|
|
477
|
+
if (arguments[@"session_id"] != nil && !self) {
|
|
478
|
+
NSString *invalid = L(@"Invalid session_id: it must identify a live registered session belonging to this MCP host. No action was taken.");
|
|
479
|
+
return TextResult([[invalid stringByAppendingFormat:@" %@", identityFailure ?: @""]
|
|
480
|
+
stringByAppendingString:IdentityNote()], YES);
|
|
481
|
+
}
|
|
428
482
|
|
|
429
483
|
if ([name isEqualToString:@"list_agents"]) {
|
|
430
484
|
NSArray<NSDictionary *> *live = CCBridgeLiveSessions();
|
|
@@ -433,8 +487,8 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
433
487
|
if (![session[@"session"] isEqual:self[@"session"]]) [sessions addObject:session];
|
|
434
488
|
}
|
|
435
489
|
NSString *header = self
|
|
436
|
-
? LF(L(@"This session is {self}; other sessions use this name to message you (it isn't in the list below)."), @{@"self":
|
|
437
|
-
: L(@"Current session identity is unavailable or ambiguous (you can view sessions, but cannot act as one).");
|
|
490
|
+
? LF(L(@"This session is {self}; other sessions use this name to message you (it isn't in the list below)."), @{@"self": CCBridgeDescribeSessionForDisplay(self)})
|
|
491
|
+
: [L(@"Current session identity is unavailable or ambiguous (you can view sessions, but cannot act as one).") stringByAppendingString:IdentityNote()];
|
|
438
492
|
if (sessions.count == 0) {
|
|
439
493
|
return TextResult(LF(L(@"{header}\n\nNo other sessions online. New sessions will show up here once they start."), @{@"header": header}), NO);
|
|
440
494
|
}
|
|
@@ -444,7 +498,7 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
444
498
|
NSMutableArray<NSString *> *lines = [NSMutableArray array];
|
|
445
499
|
for (NSDictionary *session in sessions) {
|
|
446
500
|
NSUInteger count = [counts countForObject:session[@"session"]];
|
|
447
|
-
NSString *line = [NSString stringWithFormat:@" %@ · %@ · %@ · %@ · %@ · %@",
|
|
501
|
+
NSString *line = [NSString stringWithFormat:@" %@ · %@ · %@ · %@ · %@ · %@", CCBridgeDescribeSessionForDisplay(session),
|
|
448
502
|
session[@"provider"], [session[@"status"] isEqual:@"busy"] ? @"busy" : @"idle", StringValue(session[@"tty"]) ?: @"-",
|
|
449
503
|
AgeText(session[@"startedAt"]), StringValue(session[@"cwd"]) ?: @"-"];
|
|
450
504
|
if (count > 0) line = [line stringByAppendingString:LF(L(@" · {count} reserved"), @{@"count": @(count)})];
|
|
@@ -501,7 +555,9 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
501
555
|
|
|
502
556
|
if ([name isEqualToString:@"release_files"]) {
|
|
503
557
|
if (!self) return TextResult(UnknownSelf(), YES);
|
|
504
|
-
|
|
558
|
+
NSDictionary *result = CCBridgeReleaseFiles(self, arguments[@"paths"]);
|
|
559
|
+
if (result[@"error"]) return TextResult(result[@"error"], YES);
|
|
560
|
+
NSArray<NSString *> *released = result[@"released"];
|
|
505
561
|
if (released.count == 0) return TextResult(L(@"Nothing to release."), NO);
|
|
506
562
|
NSDictionary *values = @{@"length": @(released.count), @"released": [released componentsJoinedByString:L(@", ")]};
|
|
507
563
|
return TextResult(released.count != 1 ? LF(L(@"Released {length} reservations: {released}"), values)
|
|
@@ -509,6 +565,8 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
509
565
|
}
|
|
510
566
|
|
|
511
567
|
if ([name isEqualToString:@"list_reservations"]) {
|
|
568
|
+
// 共享宿主上 MCP 进程的目录不是调用者的仓库,退回它只会查错地方。
|
|
569
|
+
if (!self) return TextResult(UnknownSelf(), YES);
|
|
512
570
|
NSString *cwd = StringValue(self[@"cwd"]) ?: NSFileManager.defaultManager.currentDirectoryPath;
|
|
513
571
|
NSString *root = CCBridgeRepoRoot(cwd);
|
|
514
572
|
NSArray<NSDictionary *> *reservations = CCBridgeActiveReservations(root, nil);
|
|
@@ -555,7 +613,7 @@ static NSDictionary *HandleRequest(NSDictionary *request, NSDictionary **error)
|
|
|
555
613
|
L(@"Find the recipient's name with list_agents, then send with send_message. "),
|
|
556
614
|
L(@"When a cross-session request needs clarification or is complete, reply to the originating session using send_message. "),
|
|
557
615
|
L(@"When several sessions work in the same repository, reserve files with reserve_files before a larger change and release them with release_files afterwards. "),
|
|
558
|
-
SafetyNote()
|
|
616
|
+
SafetyNote(), IdentityNote()
|
|
559
617
|
] componentsJoinedByString:@""]
|
|
560
618
|
};
|
|
561
619
|
}
|
|
@@ -577,7 +635,8 @@ static int MCPCommand(void) {
|
|
|
577
635
|
while ((length = getline(&line, &capacity, stdin)) >= 0) {
|
|
578
636
|
@autoreleasepool {
|
|
579
637
|
NSString *text = [[NSString alloc] initWithBytes:line length:(NSUInteger)length encoding:NSUTF8StringEncoding];
|
|
580
|
-
|
|
638
|
+
// 非法 UTF-8 时 text 为 nil,要走下面的解析错误:不回复的话客户端会一直等。
|
|
639
|
+
if (text && [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length == 0) continue;
|
|
581
640
|
NSDictionary *request = ParseJSON(text);
|
|
582
641
|
if (![request isKindOfClass:NSDictionary.class]) {
|
|
583
642
|
WriteMessage(@{@"jsonrpc": @"2.0", @"id": NSNull.null, @"error": @{@"code": @(-32700), @"message": @"Parse error"}});
|
|
@@ -703,6 +762,13 @@ static int DisableCommand(void) {
|
|
|
703
762
|
// 先关闭运行时入口,卸载耗时或失败也不能继续发送/唤醒。
|
|
704
763
|
CCBridgeSetEnabled(NO, nil);
|
|
705
764
|
BOOL succeeded = UninstallBridgeForRegisteredProfiles(CCBridgeShouldTouchMCP());
|
|
765
|
+
// 关闭后 hook 不再运行,没人会来收尾。已送达的回执留作记录,它们不含正文。
|
|
766
|
+
@try {
|
|
767
|
+
CCBridgeAbandonAllInboxes(L(@"CC Bridge is off."));
|
|
768
|
+
} @catch (NSException *exception) {
|
|
769
|
+
succeeded = NO;
|
|
770
|
+
Print(exception.reason ?: exception.name);
|
|
771
|
+
}
|
|
706
772
|
Print(succeeded ? L(@"CC Bridge is off.") : L(@"CC Bridge is off, but some integrations could not be removed. Retry disable after resolving the errors above."));
|
|
707
773
|
return succeeded ? 0 : 1;
|
|
708
774
|
}
|
|
@@ -738,9 +804,9 @@ static int StatusCommand(void) {
|
|
|
738
804
|
if (receipts.count > 0) {
|
|
739
805
|
Print(L(@"\nRecent messages:"));
|
|
740
806
|
for (NSDictionary *receipt in receipts) {
|
|
741
|
-
NSString *from = [receipt[@"from"][@"system"] boolValue] ? @"cc-pets" : receipt[@"from"]
|
|
807
|
+
NSString *from = [receipt[@"from"][@"system"] boolValue] ? @"cc-pets" : CCBridgeSessionDisplayName(receipt[@"from"]);
|
|
742
808
|
NSString *reason = StringValue(receipt[@"reason"]);
|
|
743
|
-
Print([NSString stringWithFormat:@" %@ %@ → %@ %@%@", receipt[@"id"], from, receipt[@"to"]
|
|
809
|
+
Print([NSString stringWithFormat:@" %@ %@ → %@ %@%@", receipt[@"id"], from, CCBridgeSessionDisplayName(receipt[@"to"]), receipt[@"status"],
|
|
744
810
|
reason ? LF(L(@" ({reason})"), @{@"reason": reason}) : @""]);
|
|
745
811
|
}
|
|
746
812
|
}
|
|
@@ -748,7 +814,8 @@ static int StatusCommand(void) {
|
|
|
748
814
|
}
|
|
749
815
|
|
|
750
816
|
static int SendCommand(NSMutableArray<NSString *> *arguments) {
|
|
751
|
-
|
|
817
|
+
// --from 只能写在收件人前后,再往后就是正文。
|
|
818
|
+
NSDictionary *from = SelfOrFrom(arguments, 2);
|
|
752
819
|
if (arguments.count < 2) Fail(L(@"Usage: cc-pets bridge send <to> <message…>"));
|
|
753
820
|
NSDictionary *target = CCBridgeResolveTarget(arguments[0], CCBridgeLiveSessions());
|
|
754
821
|
if (target[@"error"]) Fail(target[@"error"]);
|
|
@@ -760,7 +827,7 @@ static int SendCommand(NSMutableArray<NSString *> *arguments) {
|
|
|
760
827
|
}
|
|
761
828
|
|
|
762
829
|
static int NameCommand(NSMutableArray<NSString *> *arguments) {
|
|
763
|
-
NSDictionary *session = SelfOrFrom(arguments);
|
|
830
|
+
NSDictionary *session = SelfOrFrom(arguments, NSUIntegerMax);
|
|
764
831
|
if (arguments.count == 0) Fail(L(@"Usage: cc-pets bridge name <new-name>"));
|
|
765
832
|
NSDictionary *result = CCBridgeRenameSession(session[@"session"], arguments[0], @[]);
|
|
766
833
|
if (result[@"error"]) Fail(result[@"error"]);
|
|
@@ -769,7 +836,7 @@ static int NameCommand(NSMutableArray<NSString *> *arguments) {
|
|
|
769
836
|
}
|
|
770
837
|
|
|
771
838
|
static int InboxCommand(NSMutableArray<NSString *> *arguments) {
|
|
772
|
-
NSDictionary *session = SelfOrFrom(arguments);
|
|
839
|
+
NSDictionary *session = SelfOrFrom(arguments, NSUIntegerMax);
|
|
773
840
|
NSArray<NSDictionary *> *messages = ClaimForOutput(session[@"session"], @"check-inbox");
|
|
774
841
|
NSMutableArray<NSString *> *envelopes = [NSMutableArray array];
|
|
775
842
|
for (NSDictionary *message in messages) {
|
|
@@ -11,10 +11,16 @@
|
|
|
11
11
|
// 在线会话(不在线的顺手清出注册表),按启动时间排序。
|
|
12
12
|
NSArray<NSDictionary *> *CCBridgeLiveSessions(void);
|
|
13
13
|
NSString *CCBridgeDescribeSession(NSDictionary *session);
|
|
14
|
+
// 带 ref 和可选动物别名的展示文本;地址仍使用 CCBridgeDescribeSession。
|
|
15
|
+
NSString *CCBridgeDescribeSessionForDisplay(NSDictionary *session);
|
|
14
16
|
|
|
15
17
|
// 优先验证显式会话 ID;缺失时仅允许 provider + PID 唯一匹配。
|
|
16
18
|
// 显式 ID 失效、与宿主不符或 PID 匹配多个会话时返回 nil,禁止按活动时间猜测。
|
|
17
19
|
NSDictionary *CCBridgeIdentifySelf(void);
|
|
20
|
+
// MCP 的逐次调用身份;nil 沿用自动识别,非 nil 必须是有效 ID 且属于当前宿主。
|
|
21
|
+
// 不缓存结果,不接受名字/ref;无效显式参数不能回退到环境变量或 PID,验证不了宿主归属时同样拒绝。
|
|
22
|
+
// 拒绝显式 ID 时 failure 带上面向调用者的原因。
|
|
23
|
+
NSDictionary *CCBridgeIdentifySelfForSession(id sessionId, NSString **failure);
|
|
18
24
|
|
|
19
25
|
// 名字即地址;重名时追加 " [ref]"。也接受裸 ref 和完整 session id。返回 @{session} 或 @{error}。
|
|
20
26
|
NSDictionary *CCBridgeResolveTarget(id address, NSArray<NSDictionary *> *sessions);
|
|
@@ -36,11 +42,13 @@ extern const NSInteger CCBridgeMaximumTTLMinutes;
|
|
|
36
42
|
|
|
37
43
|
NSString *CCBridgeRepoRoot(NSString *directory);
|
|
38
44
|
// 调用方给的路径(绝对、相对、glob 均可)→ "相对仓库根、以 / 分隔"的模式;越出仓库根返回 nil。
|
|
45
|
+
// 比较前解析软链:经软链指向仓库外的路径同样算越界。
|
|
39
46
|
NSString *CCBridgeNormalizePattern(NSString *root, NSString *baseDirectory, id raw);
|
|
40
47
|
// 支持 **、*、?;不带通配符的模式视为"这个文件,或这个目录下的一切"。
|
|
41
48
|
BOOL CCBridgePatternMatches(NSString *pattern, NSString *path);
|
|
42
|
-
//
|
|
43
|
-
|
|
49
|
+
// 是否存在同时匹配两个模式的路径。不带通配符的模式可能是目录,所以连同其下的一切一起算,
|
|
50
|
+
// 除非 root 下能确认它是普通文件;root 为 nil 时不访问文件系统。
|
|
51
|
+
BOOL CCBridgePatternsOverlap(NSString *root, NSString *left, NSString *right);
|
|
44
52
|
// 过期的、持有者已不在线的预留顺手清掉,返回仍然有效的(带 holder 字段)。root 为 nil 表示所有仓库;
|
|
45
53
|
// sessions 为 nil 时现查在线会话。
|
|
46
54
|
NSArray<NSDictionary *> *CCBridgeActiveReservations(NSString *root, NSArray<NSDictionary *> *sessions);
|
|
@@ -50,7 +58,8 @@ NSString *CCBridgeDescribeReservation(NSDictionary *reservation);
|
|
|
50
58
|
NSDictionary *CCBridgeReserveFiles(NSDictionary *session, id paths, id reason, id ttlMinutes);
|
|
51
59
|
void CCBridgeAcknowledgeReservation(NSString *reservationId, NSString *sessionId);
|
|
52
60
|
BOOL CCBridgeReservationAcknowledged(NSDictionary *reservation, NSString *sessionId);
|
|
53
|
-
// 返回 @{root, released}。
|
|
61
|
+
// 返回 @{error}(paths 不是字符串数组,且未做任何改动)或 @{root, released}。
|
|
62
|
+
// 不传 paths 表示全部释放;空数组什么都不释放。
|
|
54
63
|
NSDictionary *CCBridgeReleaseFiles(NSDictionary *session, id paths);
|
|
55
64
|
// 这些具体文件是否被其他在线会话预留:[@{file, reservation}]。
|
|
56
65
|
NSArray<NSDictionary *> *CCBridgeFindForeignReservations(NSString *sessionId, NSString *cwd, NSArray<NSString *> *filePaths);
|