cc-pets 2.3.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 +63 -7
- package/CC_BRIDGE.zh-CN.md +43 -5
- package/CHANGELOG.md +106 -0
- package/CHANGELOG.zh-CN.md +106 -0
- package/CONTRIBUTING.md +23 -1
- package/CONTRIBUTING.zh-CN.md +18 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +177 -33
- package/README.zh-CN.md +137 -32
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +187 -35
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +121 -20
- 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 +14 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +3 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +118 -50
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.h +12 -2
- package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
- package/Sources/CCPets/App/main.m +6 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +45 -18
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +175 -68
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +14 -6
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +356 -110
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +247 -126
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +37 -9
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +385 -80
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +8 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +156 -25
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +11 -3
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsDoctor.m +26 -12
- package/Sources/CCPets/Support/CCPetsPreferences.h +12 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +52 -2
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +2 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +18 -7
- package/Sources/CCPets/UI/CCPetsGlassView.h +26 -4
- package/Sources/CCPets/UI/CCPetsGlassView.m +40 -9
- 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 +6 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +577 -66
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +5 -2
- package/Sources/CCPets/UI/CCPetsUIStyle.m +57 -8
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +4 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +23 -28
- package/Sources/CCPets/UI/CCPetsUsageStore.m +2 -2
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +1 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +49 -11
- package/Sources/CCPets/Usage/CCPetsUsage.h +3 -2
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +15 -17
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +11 -29
- package/Sources/CCPets/Usage/QuotaDashboardView.m +128 -2
- package/THIRD_PARTY_NOTICES.md +7 -3
- package/THIRD_PARTY_NOTICES.zh-CN.md +5 -1
- package/bin/claude-with-pet +2 -0
- package/bin/codex-with-pet +2 -0
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +24 -2
- package/scripts/compile-cached.zsh +133 -0
|
@@ -39,6 +39,32 @@ static void Print(NSString *text) {
|
|
|
39
39
|
CCPetsPrintLine(stdout, text);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// stdio MCP 顺序处理请求;认领的消息在完整输出并 flush 成功后才确认。
|
|
43
|
+
static NSDictionary *PendingInboxOutput;
|
|
44
|
+
|
|
45
|
+
static NSArray<NSDictionary *> *ClaimForOutput(NSString *session, NSString *transport) {
|
|
46
|
+
NSArray *messages = CCBridgeClaimInbox(session);
|
|
47
|
+
if (messages.count > 0) PendingInboxOutput = @{@"session": session, @"messages": messages, @"transport": transport};
|
|
48
|
+
return messages;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static void FinishInboxOutput(BOOL written) {
|
|
52
|
+
NSDictionary *pending = PendingInboxOutput;
|
|
53
|
+
PendingInboxOutput = nil;
|
|
54
|
+
if (!pending) return;
|
|
55
|
+
@try {
|
|
56
|
+
CCBridgeFinishInbox(pending[@"session"], pending[@"messages"], written ? pending[@"transport"] : nil);
|
|
57
|
+
} @catch (NSException *exception) {
|
|
58
|
+
// 输出已经完成,不能再输出第二条协议响应;未确认文件留给下次恢复。
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static BOOL WriteOutput(FILE *stream, NSString *text) {
|
|
63
|
+
NSData *data = [[text stringByAppendingString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding];
|
|
64
|
+
BOOL written = data && fwrite(data.bytes, 1, data.length, stream) == data.length;
|
|
65
|
+
return fflush(stream) == 0 && written && !ferror(stream);
|
|
66
|
+
}
|
|
67
|
+
|
|
42
68
|
static NSString *USAGE(void) {
|
|
43
69
|
return L(@"Usage: cc-pets bridge <command>\n"
|
|
44
70
|
"\n"
|
|
@@ -67,10 +93,11 @@ static NSString *USAGE(void) {
|
|
|
67
93
|
|
|
68
94
|
#pragma mark - 参数
|
|
69
95
|
|
|
70
|
-
|
|
96
|
+
// 只在前 limit 个参数里找:send 的正文可以出现任意文字,包括 --from。
|
|
97
|
+
static NSString *ParseFlagWithin(NSMutableArray<NSString *> *arguments, NSString *name, NSUInteger limit) {
|
|
71
98
|
NSString *exact = [@"--" stringByAppendingString:name];
|
|
72
99
|
NSString *prefix = [exact stringByAppendingString:@"="];
|
|
73
|
-
for (NSUInteger index = 0; index < arguments.count; index++) {
|
|
100
|
+
for (NSUInteger index = 0; index < MIN(limit, arguments.count); index++) {
|
|
74
101
|
NSString *argument = arguments[index];
|
|
75
102
|
if (![argument isEqualToString:exact] && ![argument hasPrefix:prefix]) continue;
|
|
76
103
|
[arguments removeObjectAtIndex:index];
|
|
@@ -80,11 +107,15 @@ static NSString *ParseFlag(NSMutableArray<NSString *> *arguments, NSString *name
|
|
|
80
107
|
[arguments removeObjectAtIndex:index];
|
|
81
108
|
return value;
|
|
82
109
|
}
|
|
83
|
-
|
|
110
|
+
Fail(LF(L(@"{flag} needs a value."), @{@"flag": exact}));
|
|
84
111
|
}
|
|
85
112
|
return nil;
|
|
86
113
|
}
|
|
87
114
|
|
|
115
|
+
static NSString *ParseFlag(NSMutableArray<NSString *> *arguments, NSString *name) {
|
|
116
|
+
return ParseFlagWithin(arguments, name, NSUIntegerMax);
|
|
117
|
+
}
|
|
118
|
+
|
|
88
119
|
static NSDictionary<NSString *, NSString *> *ReadOptionFlags(NSMutableArray<NSString *> *arguments) {
|
|
89
120
|
NSMutableDictionary<NSString *, NSString *> *flags = [NSMutableDictionary dictionary];
|
|
90
121
|
NSDictionary<NSString *, NSString *> *names = @{@"approve": @"approve", @"codex-approve": @"codexApprove",
|
|
@@ -100,6 +131,8 @@ static NSDictionary *MergedOptions(NSDictionary *base, NSMutableArray<NSString *
|
|
|
100
131
|
NSString *error = nil;
|
|
101
132
|
NSDictionary *options = CCBridgeMergeOptionFlags(base, ReadOptionFlags(arguments), &error);
|
|
102
133
|
if (!options) Fail(error);
|
|
134
|
+
// 拼错的选项不能当成"没给":用户以为改了,其实什么都没变。
|
|
135
|
+
if (arguments.count > 0) Fail(LF(L(@"Unknown option: {option}"), @{@"option": arguments.firstObject}));
|
|
103
136
|
return options;
|
|
104
137
|
}
|
|
105
138
|
|
|
@@ -110,35 +143,44 @@ static void PrintSessions(void) {
|
|
|
110
143
|
return;
|
|
111
144
|
}
|
|
112
145
|
for (NSDictionary *session in sessions) {
|
|
113
|
-
Print([NSString stringWithFormat:@"%@ %@ %@ pid=%@ %@ %@ %@=%lu",
|
|
146
|
+
Print([NSString stringWithFormat:@"%@ %@ %@ pid=%@ %@ %@ %@=%lu", CCBridgeDescribeSessionForDisplay(session), session[@"provider"],
|
|
114
147
|
StringValue(session[@"status"]) ?: @"-", session[@"pid"] ?: @"-", StringValue(session[@"tty"]) ?: @"-",
|
|
115
148
|
StringValue(session[@"cwd"]) ?: @"-", L(@"pending"), (unsigned long)CCBridgePendingCount(session[@"session"])]);
|
|
116
149
|
}
|
|
117
150
|
}
|
|
118
151
|
|
|
119
|
-
static NSDictionary *SelfOrFrom(NSMutableArray<NSString *> *arguments) {
|
|
120
|
-
|
|
152
|
+
static NSDictionary *SelfOrFrom(NSMutableArray<NSString *> *arguments, NSUInteger flagLimit) {
|
|
153
|
+
if (!CCBridgeEnabled()) Fail(L(@"CC Bridge is off. Run cc-pets bridge enable first."));
|
|
154
|
+
NSString *from = ParseFlagWithin(arguments, @"from", flagLimit);
|
|
121
155
|
if (from.length > 0) {
|
|
122
156
|
NSDictionary *resolved = CCBridgeResolveTarget(from, CCBridgeLiveSessions());
|
|
123
157
|
if (resolved[@"error"]) Fail(resolved[@"error"]);
|
|
124
158
|
return resolved[@"session"];
|
|
125
159
|
}
|
|
126
160
|
NSDictionary *session = CCBridgeIdentifySelf();
|
|
127
|
-
if (!session) Fail(L(@"
|
|
161
|
+
if (!session) Fail(L(@"Current session identity is unavailable or ambiguous; use a valid session ID, or --from <session-name> for an explicit CLI selection."));
|
|
128
162
|
return session;
|
|
129
163
|
}
|
|
130
164
|
|
|
131
165
|
#pragma mark - hook
|
|
132
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
|
+
|
|
133
176
|
static NSDictionary *InjectPending(NSString *sessionId, NSString *eventName) {
|
|
134
|
-
NSArray<NSDictionary *> *messages =
|
|
135
|
-
|
|
136
|
-
NSMutableArray<NSString *> *envelopes = [NSMutableArray
|
|
177
|
+
NSArray<NSDictionary *> *messages = ClaimForOutput(sessionId, @"prompt-inject");
|
|
178
|
+
// 每次用户输入都重新提供身份,覆盖 resume、上下文压缩和 MCP 重新连接。
|
|
179
|
+
NSMutableArray<NSString *> *envelopes = [NSMutableArray arrayWithObject:SessionIdentityContext(sessionId)];
|
|
137
180
|
for (NSDictionary *message in messages) {
|
|
138
|
-
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"delivered", @"transport": @{@"kind": @"prompt-inject"}});
|
|
139
181
|
[envelopes addObject:CCBridgeFormatEnvelope(message)];
|
|
140
182
|
}
|
|
141
|
-
return
|
|
183
|
+
return HookContext(eventName, [envelopes componentsJoinedByString:@"\n\n"]);
|
|
142
184
|
}
|
|
143
185
|
|
|
144
186
|
// 首次拦截、重试放行:本会话第一次编辑某处他人预留的文件时返回 deny 并说明原因,同时记下"已提醒";
|
|
@@ -180,15 +222,25 @@ static NSDictionary *RunHook(NSString *input) {
|
|
|
180
222
|
if (event.length == 0 || !sessionId) return nil;
|
|
181
223
|
|
|
182
224
|
if ([event isEqualToString:@"SessionEnd"]) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
+
}
|
|
186
235
|
return nil;
|
|
187
236
|
}
|
|
188
237
|
// 编辑前检查文件预留。不更新注册表——这个 hook 每次编辑都会跑,保持最轻。
|
|
189
238
|
if ([event isEqualToString:@"PreToolUse"]) {
|
|
190
239
|
return [CCBridgeOptions()[@"editGuard"] boolValue] ? CheckReservations(payload, sessionId) : nil;
|
|
191
240
|
}
|
|
241
|
+
// 没有持久化转录的 Codex 临时任务不能作为 queue 的收件会话,但仍需检查文件预留。
|
|
242
|
+
// 缺字段的旧版 hook 保持兼容;SessionEnd 仍先走上面的清理。
|
|
243
|
+
if ([provider isEqualToString:@"Codex"] && payload[@"transcript_path"] == NSNull.null) return nil;
|
|
192
244
|
|
|
193
245
|
NSString *status = @{@"SessionStart": @"idle", @"UserPromptSubmit": @"busy", @"Stop": @"idle"}[event];
|
|
194
246
|
NSDictionary *known = CCBridgeReadSession(sessionId);
|
|
@@ -201,11 +253,16 @@ static NSDictionary *RunHook(NSString *input) {
|
|
|
201
253
|
pid ? CCBridgeProcessTty(pid.intValue) : nil, status,
|
|
202
254
|
// hook 继承宿主 CLI 的环境:`CC_BRIDGE_NAME=frontend claude` 即以 frontend 注册。只在首次注册时生效。
|
|
203
255
|
preferredName, CCPetsEnvironmentValue(@"CC_PETS_PROFILE_ID"),
|
|
204
|
-
CCPetsEnvironmentValue(@"CC_PETS_PROFILE_NAME"),
|
|
256
|
+
CCPetsEnvironmentValue(@"CC_PETS_PROFILE_NAME"), @[]);
|
|
205
257
|
|
|
206
258
|
if ([event isEqualToString:@"SessionStart"]) {
|
|
207
|
-
|
|
208
|
-
|
|
259
|
+
// 清理失败不能影响身份注入。
|
|
260
|
+
@try {
|
|
261
|
+
CCBridgeSweepMessages();
|
|
262
|
+
CCBridgePruneReceipts();
|
|
263
|
+
} @catch (NSException *exception) {
|
|
264
|
+
}
|
|
265
|
+
return HookContext(event, SessionIdentityContext(sessionId));
|
|
209
266
|
}
|
|
210
267
|
if ([event isEqualToString:@"UserPromptSubmit"]) return InjectPending(sessionId, event);
|
|
211
268
|
// 空闲通知可能要调 codex queue(秒级),放到脱离的子进程里做,不让 Stop hook 拖慢对方回合的收尾。
|
|
@@ -217,13 +274,15 @@ static NSDictionary *RunHook(NSString *input) {
|
|
|
217
274
|
|
|
218
275
|
// 这是别人 CLI 的关键路径:任何异常都吞掉并 exit 0,CC Bridge 出错不能拖垮对方的一轮对话。
|
|
219
276
|
static int HookCommand(void) {
|
|
277
|
+
BOOL written = NO;
|
|
220
278
|
@try {
|
|
221
279
|
NSDictionary *output = RunHook(ReadStandardInput());
|
|
222
280
|
if (output) {
|
|
223
|
-
|
|
224
|
-
fflush(stdout);
|
|
281
|
+
written = WriteOutput(stdout, CompactJSON(output));
|
|
225
282
|
}
|
|
226
283
|
} @catch (NSException *exception) {
|
|
284
|
+
} @finally {
|
|
285
|
+
FinishInboxOutput(written);
|
|
227
286
|
}
|
|
228
287
|
return 0;
|
|
229
288
|
}
|
|
@@ -231,18 +290,19 @@ static int HookCommand(void) {
|
|
|
231
290
|
#pragma mark - watch
|
|
232
291
|
// Claude 端的投递者:以 asyncRewake hook 运行,等信箱来消息后 exit 2 唤醒会话。实测:exit 2 时 stderr
|
|
233
292
|
// 作为系统提醒进入会话,空闲会被唤醒、忙碌则注入当前回合;hook 的 timeout 会被强制执行,所以 watcher
|
|
234
|
-
// 在超时前主动退出,下一次事件再重新挂上。每个会话只保留一个 watcher
|
|
293
|
+
// 在超时前主动退出,下一次事件再重新挂上。每个会话只保留一个 watcher(锁文件):新挂上的取代旧的,
|
|
294
|
+
// 并从自己启动时重新计时,否则活跃的会话也会在第一个 watcher 到期后断掉唤醒,直到下一轮结束。
|
|
235
295
|
|
|
236
296
|
static int WatchCommand(void) {
|
|
237
297
|
NSString *lock = nil;
|
|
298
|
+
BOOL written = NO;
|
|
238
299
|
@try {
|
|
239
300
|
// 关闭"自动唤醒"后 watcher 本不会被安装;配置还没刷新到对方 CLI 时在这里兜底。
|
|
240
301
|
if (!CCBridgeEnabled() || ![CCBridgeOptions()[@"wake"] boolValue]) return 0;
|
|
241
302
|
NSDictionary *payload = ParseJSON(ReadStandardInput());
|
|
242
303
|
NSString *sessionId = [payload isKindOfClass:NSDictionary.class] ? CCBridgeSafeSessionId(payload[@"session_id"]) : nil;
|
|
243
304
|
if (!sessionId) return 0;
|
|
244
|
-
|
|
245
|
-
if (!lock) return 0;
|
|
305
|
+
NSString *token = CCBridgeClaimWatcher(sessionId);
|
|
246
306
|
// 安装时 hook 的 timeout 是 3600 秒,这里留 5 分钟余量主动退出,保证锁由自己释放。
|
|
247
307
|
double seconds = CCPetsEnvironmentValue(@"CC_BRIDGE_WATCH_SECONDS").doubleValue;
|
|
248
308
|
if (!(seconds > 0)) seconds = 3300;
|
|
@@ -251,17 +311,27 @@ static int WatchCommand(void) {
|
|
|
251
311
|
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:seconds];
|
|
252
312
|
pid_t parent = getppid();
|
|
253
313
|
while (deadline.timeIntervalSinceNow > 0) {
|
|
314
|
+
if (!CCBridgeEnabled() || ![CCBridgeOptions()[@"wake"] boolValue]) return 0;
|
|
254
315
|
// 宿主会话已经没了(被 kill -9 等,没来得及清理子进程)就别再占着锁。
|
|
255
316
|
if (!CCBridgeProcessAlive(parent)) return 0;
|
|
256
|
-
|
|
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") : @[];
|
|
257
327
|
if (messages.count > 0) {
|
|
258
328
|
NSMutableArray<NSString *> *envelopes = [NSMutableArray array];
|
|
259
329
|
for (NSDictionary *message in messages) {
|
|
260
|
-
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"delivered", @"transport": @{@"kind": @"claude-rewake"}});
|
|
261
330
|
[envelopes addObject:CCBridgeFormatEnvelope(message)];
|
|
262
331
|
}
|
|
263
|
-
|
|
264
|
-
|
|
332
|
+
if (!CCBridgeEnabled() || ![CCBridgeOptions()[@"wake"] boolValue]) return 0;
|
|
333
|
+
written = WriteOutput(stderr, [envelopes componentsJoinedByString:@"\n\n"]);
|
|
334
|
+
return written ? 2 : 0;
|
|
265
335
|
}
|
|
266
336
|
usleep((useconds_t)(poll * 1000));
|
|
267
337
|
}
|
|
@@ -269,6 +339,7 @@ static int WatchCommand(void) {
|
|
|
269
339
|
} @catch (NSException *exception) {
|
|
270
340
|
return 0;
|
|
271
341
|
} @finally {
|
|
342
|
+
FinishInboxOutput(written);
|
|
272
343
|
CCBridgeReleaseLock(lock);
|
|
273
344
|
}
|
|
274
345
|
}
|
|
@@ -295,7 +366,10 @@ static NSString *AliasNote(void) {
|
|
|
295
366
|
}
|
|
296
367
|
|
|
297
368
|
static NSDictionary *Schema(NSDictionary *properties, NSArray<NSString *> *required) {
|
|
298
|
-
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];
|
|
299
373
|
if (required) schema[@"required"] = required;
|
|
300
374
|
return schema;
|
|
301
375
|
}
|
|
@@ -324,7 +398,7 @@ static NSArray<NSDictionary *> *Tools(void) {
|
|
|
324
398
|
@"message": @{@"type": @"string",
|
|
325
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.")},
|
|
326
400
|
@"notify_when_idle": @{@"type": @"boolean",
|
|
327
|
-
@"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.")}
|
|
328
402
|
}, @[@"to", @"message"]),
|
|
329
403
|
@"annotations": @{@"readOnlyHint": @NO, @"destructiveHint": @NO, @"title": L(@"Send cross-session message")}},
|
|
330
404
|
@{@"name": @"set_name",
|
|
@@ -384,9 +458,12 @@ static NSString *AgeText(id since) {
|
|
|
384
458
|
return LF(L(@"started {minutes} h ago"), @{@"minutes": @((long long)floor(minutes / 60 + 0.5))});
|
|
385
459
|
}
|
|
386
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
|
+
|
|
387
465
|
static NSString *UnknownSelf(void) {
|
|
388
|
-
return [L(@"
|
|
389
|
-
stringByAppendingString:L(@"Codex users also need to trust the cc-pets hooks in /hooks and send at least one message in this session.")];
|
|
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()];
|
|
390
467
|
}
|
|
391
468
|
|
|
392
469
|
static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
@@ -394,7 +471,14 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
394
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);
|
|
395
472
|
}
|
|
396
473
|
if (![arguments isKindOfClass:NSDictionary.class]) arguments = @{};
|
|
397
|
-
|
|
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
|
+
}
|
|
398
482
|
|
|
399
483
|
if ([name isEqualToString:@"list_agents"]) {
|
|
400
484
|
NSArray<NSDictionary *> *live = CCBridgeLiveSessions();
|
|
@@ -403,8 +487,8 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
403
487
|
if (![session[@"session"] isEqual:self[@"session"]]) [sessions addObject:session];
|
|
404
488
|
}
|
|
405
489
|
NSString *header = self
|
|
406
|
-
? LF(L(@"This session is {self}; other sessions use this name to message you (it isn't in the list below)."), @{@"self":
|
|
407
|
-
: L(@"
|
|
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()];
|
|
408
492
|
if (sessions.count == 0) {
|
|
409
493
|
return TextResult(LF(L(@"{header}\n\nNo other sessions online. New sessions will show up here once they start."), @{@"header": header}), NO);
|
|
410
494
|
}
|
|
@@ -414,7 +498,7 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
414
498
|
NSMutableArray<NSString *> *lines = [NSMutableArray array];
|
|
415
499
|
for (NSDictionary *session in sessions) {
|
|
416
500
|
NSUInteger count = [counts countForObject:session[@"session"]];
|
|
417
|
-
NSString *line = [NSString stringWithFormat:@" %@ · %@ · %@ · %@ · %@ · %@",
|
|
501
|
+
NSString *line = [NSString stringWithFormat:@" %@ · %@ · %@ · %@ · %@ · %@", CCBridgeDescribeSessionForDisplay(session),
|
|
418
502
|
session[@"provider"], [session[@"status"] isEqual:@"busy"] ? @"busy" : @"idle", StringValue(session[@"tty"]) ?: @"-",
|
|
419
503
|
AgeText(session[@"startedAt"]), StringValue(session[@"cwd"]) ?: @"-"];
|
|
420
504
|
if (count > 0) line = [line stringByAppendingString:LF(L(@" · {count} reserved"), @{@"count": @(count)})];
|
|
@@ -440,7 +524,7 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
440
524
|
|
|
441
525
|
if ([name isEqualToString:@"set_name"]) {
|
|
442
526
|
if (!self) return TextResult(UnknownSelf(), YES);
|
|
443
|
-
NSDictionary *result = CCBridgeRenameSession(self[@"session"], arguments[@"name"],
|
|
527
|
+
NSDictionary *result = CCBridgeRenameSession(self[@"session"], arguments[@"name"], @[]);
|
|
444
528
|
if (result[@"error"]) return TextResult(result[@"error"], YES);
|
|
445
529
|
return TextResult([LF(L(@"This session was renamed from {previous} to {session}. "),
|
|
446
530
|
(@{@"previous": result[@"previous"], @"session": CCBridgeDescribeSession(result[@"session"])}))
|
|
@@ -471,7 +555,9 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
471
555
|
|
|
472
556
|
if ([name isEqualToString:@"release_files"]) {
|
|
473
557
|
if (!self) return TextResult(UnknownSelf(), YES);
|
|
474
|
-
|
|
558
|
+
NSDictionary *result = CCBridgeReleaseFiles(self, arguments[@"paths"]);
|
|
559
|
+
if (result[@"error"]) return TextResult(result[@"error"], YES);
|
|
560
|
+
NSArray<NSString *> *released = result[@"released"];
|
|
475
561
|
if (released.count == 0) return TextResult(L(@"Nothing to release."), NO);
|
|
476
562
|
NSDictionary *values = @{@"length": @(released.count), @"released": [released componentsJoinedByString:L(@", ")]};
|
|
477
563
|
return TextResult(released.count != 1 ? LF(L(@"Released {length} reservations: {released}"), values)
|
|
@@ -479,6 +565,8 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
479
565
|
}
|
|
480
566
|
|
|
481
567
|
if ([name isEqualToString:@"list_reservations"]) {
|
|
568
|
+
// 共享宿主上 MCP 进程的目录不是调用者的仓库,退回它只会查错地方。
|
|
569
|
+
if (!self) return TextResult(UnknownSelf(), YES);
|
|
482
570
|
NSString *cwd = StringValue(self[@"cwd"]) ?: NSFileManager.defaultManager.currentDirectoryPath;
|
|
483
571
|
NSString *root = CCBridgeRepoRoot(cwd);
|
|
484
572
|
NSArray<NSDictionary *> *reservations = CCBridgeActiveReservations(root, nil);
|
|
@@ -497,11 +585,10 @@ static NSDictionary *CallTool(NSString *name, NSDictionary *arguments) {
|
|
|
497
585
|
|
|
498
586
|
if ([name isEqualToString:@"check_inbox"]) {
|
|
499
587
|
if (!self) return TextResult(UnknownSelf(), YES);
|
|
500
|
-
NSArray<NSDictionary *> *messages =
|
|
588
|
+
NSArray<NSDictionary *> *messages = ClaimForOutput(self[@"session"], @"check-inbox");
|
|
501
589
|
if (messages.count == 0) return TextResult(L(@"No new messages in the inbox."), NO);
|
|
502
590
|
NSMutableArray<NSString *> *envelopes = [NSMutableArray array];
|
|
503
591
|
for (NSDictionary *message in messages) {
|
|
504
|
-
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"delivered", @"transport": @{@"kind": @"check-inbox"}});
|
|
505
592
|
[envelopes addObject:CCBridgeFormatEnvelope(message)];
|
|
506
593
|
}
|
|
507
594
|
return TextResult([envelopes componentsJoinedByString:@"\n\n"], NO);
|
|
@@ -524,8 +611,9 @@ static NSDictionary *HandleRequest(NSDictionary *request, NSDictionary **error)
|
|
|
524
611
|
L(@"“message another session / terminal / Claude / Codex”, or names a session or tty (e.g. ttys003), "),
|
|
525
612
|
L(@"use this server's tools directly; there's no need to look up bridge usage in project files. "), AliasNote(),
|
|
526
613
|
L(@"Find the recipient's name with list_agents, then send with send_message. "),
|
|
614
|
+
L(@"When a cross-session request needs clarification or is complete, reply to the originating session using send_message. "),
|
|
527
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. "),
|
|
528
|
-
SafetyNote()
|
|
616
|
+
SafetyNote(), IdentityNote()
|
|
529
617
|
] componentsJoinedByString:@""]
|
|
530
618
|
};
|
|
531
619
|
}
|
|
@@ -536,9 +624,8 @@ static NSDictionary *HandleRequest(NSDictionary *request, NSDictionary **error)
|
|
|
536
624
|
return nil;
|
|
537
625
|
}
|
|
538
626
|
|
|
539
|
-
static
|
|
540
|
-
|
|
541
|
-
fflush(stdout);
|
|
627
|
+
static BOOL WriteMessage(NSDictionary *message) {
|
|
628
|
+
return WriteOutput(stdout, CompactJSON(message));
|
|
542
629
|
}
|
|
543
630
|
|
|
544
631
|
static int MCPCommand(void) {
|
|
@@ -548,7 +635,8 @@ static int MCPCommand(void) {
|
|
|
548
635
|
while ((length = getline(&line, &capacity, stdin)) >= 0) {
|
|
549
636
|
@autoreleasepool {
|
|
550
637
|
NSString *text = [[NSString alloc] initWithBytes:line length:(NSUInteger)length encoding:NSUTF8StringEncoding];
|
|
551
|
-
|
|
638
|
+
// 非法 UTF-8 时 text 为 nil,要走下面的解析错误:不回复的话客户端会一直等。
|
|
639
|
+
if (text && [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length == 0) continue;
|
|
552
640
|
NSDictionary *request = ParseJSON(text);
|
|
553
641
|
if (![request isKindOfClass:NSDictionary.class]) {
|
|
554
642
|
WriteMessage(@{@"jsonrpc": @"2.0", @"id": NSNull.null, @"error": @{@"code": @(-32700), @"message": @"Parse error"}});
|
|
@@ -564,8 +652,14 @@ static int MCPCommand(void) {
|
|
|
564
652
|
} @catch (NSException *exception) {
|
|
565
653
|
error = @{@"code": @(-32603), @"message": exception.reason ?: exception.name};
|
|
566
654
|
}
|
|
567
|
-
|
|
568
|
-
|
|
655
|
+
BOOL written = NO;
|
|
656
|
+
@try {
|
|
657
|
+
written = WriteMessage(error ? @{@"jsonrpc": @"2.0", @"id": identifier, @"error": error}
|
|
658
|
+
: @{@"jsonrpc": @"2.0", @"id": identifier, @"result": result ?: @{}});
|
|
659
|
+
} @finally {
|
|
660
|
+
FinishInboxOutput(written && !error);
|
|
661
|
+
}
|
|
662
|
+
if (!written) { free(line); return 1; }
|
|
569
663
|
}
|
|
570
664
|
}
|
|
571
665
|
free(line);
|
|
@@ -610,19 +704,25 @@ static BOOL InstallBridgeForRegisteredProfiles(NSDictionary *options, BOOL regis
|
|
|
610
704
|
return succeeded;
|
|
611
705
|
}
|
|
612
706
|
|
|
613
|
-
static
|
|
707
|
+
static BOOL UninstallBridgeForRegisteredProfiles(BOOL unregisterMcp) {
|
|
708
|
+
BOOL succeeded = YES;
|
|
614
709
|
NSArray<NSDictionary *> *profiles = RegisteredProfiles();
|
|
615
710
|
if (profiles.count == 0) {
|
|
616
|
-
return;
|
|
711
|
+
return YES;
|
|
617
712
|
}
|
|
618
713
|
for (NSDictionary *profile in profiles) {
|
|
619
714
|
@try {
|
|
620
|
-
for (NSString *line in CCBridgeUninstallForProfile(profile, unregisterMcp))
|
|
715
|
+
for (NSString *line in CCBridgeUninstallForProfile(profile, unregisterMcp)) {
|
|
716
|
+
Print(line);
|
|
717
|
+
if ([line hasPrefix:@"ERROR: "]) succeeded = NO;
|
|
718
|
+
}
|
|
621
719
|
} @catch (NSException *exception) {
|
|
720
|
+
succeeded = NO;
|
|
622
721
|
Print(LF(L(@"{name}: {error}"), (@{@"name": profile[@"displayName"] ?: profile[@"home"],
|
|
623
722
|
@"error": exception.reason ?: exception.name})));
|
|
624
723
|
}
|
|
625
724
|
}
|
|
725
|
+
return succeeded;
|
|
626
726
|
}
|
|
627
727
|
|
|
628
728
|
static int EnableCommand(NSMutableArray<NSString *> *arguments) {
|
|
@@ -659,19 +759,23 @@ static int RefreshCommand(void) {
|
|
|
659
759
|
}
|
|
660
760
|
|
|
661
761
|
static int DisableCommand(void) {
|
|
662
|
-
|
|
762
|
+
// 先关闭运行时入口,卸载耗时或失败也不能继续发送/唤醒。
|
|
663
763
|
CCBridgeSetEnabled(NO, nil);
|
|
664
|
-
|
|
665
|
-
|
|
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
|
+
}
|
|
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."));
|
|
773
|
+
return succeeded ? 0 : 1;
|
|
666
774
|
}
|
|
667
775
|
|
|
668
|
-
//
|
|
669
|
-
// 避免卸载时无谓地拉起 claude / codex。
|
|
776
|
+
// 即使之前卸载失败已关掉开关,再次 teardown 也必须重试清理 MCP。
|
|
670
777
|
static int TeardownCommand(void) {
|
|
671
|
-
|
|
672
|
-
UninstallBridgeForRegisteredProfiles(wasEnabled && CCBridgeShouldTouchMCP());
|
|
673
|
-
CCBridgeSetEnabled(NO, nil);
|
|
674
|
-
return 0;
|
|
778
|
+
return DisableCommand();
|
|
675
779
|
}
|
|
676
780
|
|
|
677
781
|
static int StatusCommand(void) {
|
|
@@ -700,9 +804,9 @@ static int StatusCommand(void) {
|
|
|
700
804
|
if (receipts.count > 0) {
|
|
701
805
|
Print(L(@"\nRecent messages:"));
|
|
702
806
|
for (NSDictionary *receipt in receipts) {
|
|
703
|
-
NSString *from = [receipt[@"from"][@"system"] boolValue] ? @"cc-pets" : receipt[@"from"]
|
|
807
|
+
NSString *from = [receipt[@"from"][@"system"] boolValue] ? @"cc-pets" : CCBridgeSessionDisplayName(receipt[@"from"]);
|
|
704
808
|
NSString *reason = StringValue(receipt[@"reason"]);
|
|
705
|
-
Print([NSString stringWithFormat:@" %@ %@ → %@ %@%@", receipt[@"id"], from, receipt[@"to"]
|
|
809
|
+
Print([NSString stringWithFormat:@" %@ %@ → %@ %@%@", receipt[@"id"], from, CCBridgeSessionDisplayName(receipt[@"to"]), receipt[@"status"],
|
|
706
810
|
reason ? LF(L(@" ({reason})"), @{@"reason": reason}) : @""]);
|
|
707
811
|
}
|
|
708
812
|
}
|
|
@@ -710,7 +814,8 @@ static int StatusCommand(void) {
|
|
|
710
814
|
}
|
|
711
815
|
|
|
712
816
|
static int SendCommand(NSMutableArray<NSString *> *arguments) {
|
|
713
|
-
|
|
817
|
+
// --from 只能写在收件人前后,再往后就是正文。
|
|
818
|
+
NSDictionary *from = SelfOrFrom(arguments, 2);
|
|
714
819
|
if (arguments.count < 2) Fail(L(@"Usage: cc-pets bridge send <to> <message…>"));
|
|
715
820
|
NSDictionary *target = CCBridgeResolveTarget(arguments[0], CCBridgeLiveSessions());
|
|
716
821
|
if (target[@"error"]) Fail(target[@"error"]);
|
|
@@ -722,24 +827,24 @@ static int SendCommand(NSMutableArray<NSString *> *arguments) {
|
|
|
722
827
|
}
|
|
723
828
|
|
|
724
829
|
static int NameCommand(NSMutableArray<NSString *> *arguments) {
|
|
725
|
-
NSDictionary *session = SelfOrFrom(arguments);
|
|
830
|
+
NSDictionary *session = SelfOrFrom(arguments, NSUIntegerMax);
|
|
726
831
|
if (arguments.count == 0) Fail(L(@"Usage: cc-pets bridge name <new-name>"));
|
|
727
|
-
NSDictionary *result = CCBridgeRenameSession(session[@"session"], arguments[0],
|
|
832
|
+
NSDictionary *result = CCBridgeRenameSession(session[@"session"], arguments[0], @[]);
|
|
728
833
|
if (result[@"error"]) Fail(result[@"error"]);
|
|
729
834
|
Print(LF(L(@"Renamed {previous} to {session}."), (@{@"previous": result[@"previous"], @"session": CCBridgeDescribeSession(result[@"session"])})));
|
|
730
835
|
return 0;
|
|
731
836
|
}
|
|
732
837
|
|
|
733
838
|
static int InboxCommand(NSMutableArray<NSString *> *arguments) {
|
|
734
|
-
NSDictionary *session = SelfOrFrom(arguments);
|
|
735
|
-
NSArray<NSDictionary *> *messages =
|
|
839
|
+
NSDictionary *session = SelfOrFrom(arguments, NSUIntegerMax);
|
|
840
|
+
NSArray<NSDictionary *> *messages = ClaimForOutput(session[@"session"], @"check-inbox");
|
|
736
841
|
NSMutableArray<NSString *> *envelopes = [NSMutableArray array];
|
|
737
842
|
for (NSDictionary *message in messages) {
|
|
738
|
-
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"delivered", @"transport": @{@"kind": @"check-inbox"}});
|
|
739
843
|
[envelopes addObject:CCBridgeFormatEnvelope(message)];
|
|
740
844
|
}
|
|
741
|
-
|
|
742
|
-
|
|
845
|
+
BOOL written = WriteOutput(stdout, envelopes.count > 0 ? [envelopes componentsJoinedByString:@"\n\n"] : L(@"No new messages in the inbox."));
|
|
846
|
+
FinishInboxOutput(written);
|
|
847
|
+
return written ? 0 : 1;
|
|
743
848
|
}
|
|
744
849
|
|
|
745
850
|
// Stop hook 脱离出来的子进程:给订阅了该会话空闲的会话发通知。
|
|
@@ -788,5 +893,7 @@ int CCBridgeCommandMain(NSArray<NSString *> *argv) {
|
|
|
788
893
|
} @catch (NSException *exception) {
|
|
789
894
|
CCPetsPrintLine(stderr, exception.reason ?: exception.name);
|
|
790
895
|
return 1;
|
|
896
|
+
} @finally {
|
|
897
|
+
FinishInboxOutput(NO);
|
|
791
898
|
}
|
|
792
899
|
}
|
|
@@ -11,11 +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
|
-
//
|
|
16
|
-
//
|
|
17
|
-
// /clear、/resume 之后会过期。反查不到时才退回环境变量。
|
|
17
|
+
// 优先验证显式会话 ID;缺失时仅允许 provider + PID 唯一匹配。
|
|
18
|
+
// 显式 ID 失效、与宿主不符或 PID 匹配多个会话时返回 nil,禁止按活动时间猜测。
|
|
18
19
|
NSDictionary *CCBridgeIdentifySelf(void);
|
|
20
|
+
// MCP 的逐次调用身份;nil 沿用自动识别,非 nil 必须是有效 ID 且属于当前宿主。
|
|
21
|
+
// 不缓存结果,不接受名字/ref;无效显式参数不能回退到环境变量或 PID,验证不了宿主归属时同样拒绝。
|
|
22
|
+
// 拒绝显式 ID 时 failure 带上面向调用者的原因。
|
|
23
|
+
NSDictionary *CCBridgeIdentifySelfForSession(id sessionId, NSString **failure);
|
|
19
24
|
|
|
20
25
|
// 名字即地址;重名时追加 " [ref]"。也接受裸 ref 和完整 session id。返回 @{session} 或 @{error}。
|
|
21
26
|
NSDictionary *CCBridgeResolveTarget(id address, NSArray<NSDictionary *> *sessions);
|
|
@@ -37,11 +42,13 @@ extern const NSInteger CCBridgeMaximumTTLMinutes;
|
|
|
37
42
|
|
|
38
43
|
NSString *CCBridgeRepoRoot(NSString *directory);
|
|
39
44
|
// 调用方给的路径(绝对、相对、glob 均可)→ "相对仓库根、以 / 分隔"的模式;越出仓库根返回 nil。
|
|
45
|
+
// 比较前解析软链:经软链指向仓库外的路径同样算越界。
|
|
40
46
|
NSString *CCBridgeNormalizePattern(NSString *root, NSString *baseDirectory, id raw);
|
|
41
47
|
// 支持 **、*、?;不带通配符的模式视为"这个文件,或这个目录下的一切"。
|
|
42
48
|
BOOL CCBridgePatternMatches(NSString *pattern, NSString *path);
|
|
43
|
-
//
|
|
44
|
-
|
|
49
|
+
// 是否存在同时匹配两个模式的路径。不带通配符的模式可能是目录,所以连同其下的一切一起算,
|
|
50
|
+
// 除非 root 下能确认它是普通文件;root 为 nil 时不访问文件系统。
|
|
51
|
+
BOOL CCBridgePatternsOverlap(NSString *root, NSString *left, NSString *right);
|
|
45
52
|
// 过期的、持有者已不在线的预留顺手清掉,返回仍然有效的(带 holder 字段)。root 为 nil 表示所有仓库;
|
|
46
53
|
// sessions 为 nil 时现查在线会话。
|
|
47
54
|
NSArray<NSDictionary *> *CCBridgeActiveReservations(NSString *root, NSArray<NSDictionary *> *sessions);
|
|
@@ -51,7 +58,8 @@ NSString *CCBridgeDescribeReservation(NSDictionary *reservation);
|
|
|
51
58
|
NSDictionary *CCBridgeReserveFiles(NSDictionary *session, id paths, id reason, id ttlMinutes);
|
|
52
59
|
void CCBridgeAcknowledgeReservation(NSString *reservationId, NSString *sessionId);
|
|
53
60
|
BOOL CCBridgeReservationAcknowledged(NSDictionary *reservation, NSString *sessionId);
|
|
54
|
-
// 返回 @{root, released}。
|
|
61
|
+
// 返回 @{error}(paths 不是字符串数组,且未做任何改动)或 @{root, released}。
|
|
62
|
+
// 不传 paths 表示全部释放;空数组什么都不释放。
|
|
55
63
|
NSDictionary *CCBridgeReleaseFiles(NSDictionary *session, id paths);
|
|
56
64
|
// 这些具体文件是否被其他在线会话预留:[@{file, reservation}]。
|
|
57
65
|
NSArray<NSDictionary *> *CCBridgeFindForeignReservations(NSString *sessionId, NSString *cwd, NSArray<NSString *> *filePaths);
|