remoteServiceAccess.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import { IGenUiService } from "@zcode/services";
  2. import { ProxyChannel, type IChannelClient } from "@zcode/rpc";
  3. import {
  4. IFileService,
  5. IMediaPreviewService,
  6. IGitService,
  7. IGitCheckpointService,
  8. ISystemService,
  9. ITerminalService,
  10. ISettingService,
  11. IOnboardingRecordService,
  12. ICredentialService,
  13. IBroadcastService,
  14. IZCodeTaskService,
  15. IZCodeAgentService,
  16. IZCodeSessionService,
  17. IProjectService,
  18. IManagedWorktreeService,
  19. ITaskGroupService,
  20. IRemoteProjectService,
  21. ICuaPermissionService,
  22. IConversationShareService,
  23. IBotsService,
  24. IFileWatcherService,
  25. IOAuthService,
  26. IModelSelectionService,
  27. IProviderSettingsService,
  28. IProviderProvisioningTargetService,
  29. ISourceServerWorkspaceService,
  30. ISourceServerManagementService,
  31. IDeviceSyncStateService,
  32. IProviderProvisioningStatusService,
  33. IExtensionsService,
  34. IExtensionViewsService,
  35. IExtensionGrantsService,
  36. IVoiceInputService,
  37. IUsageStatsService,
  38. ICodingPlanSubscriptionService,
  39. IClientConfigService,
  40. IClientScenesService,
  41. ICloudContentService,
  42. IMarketingTouchService,
  43. IOffPeakTaskService,
  44. IHighspeedCardService,
  45. ISkillsService,
  46. ISkillSyncService,
  47. IUserConfigSyncService,
  48. IMcpSyncService,
  49. IPluginUiAppToolsService,
  50. IPluginUiSamplingService,
  51. IPluginUiBridgeService,
  52. IPluginSyncService,
  53. IPluginsService,
  54. IPluginManagementService,
  55. ISubagentsService,
  56. ICommandsService,
  57. IHooksService,
  58. IMemoryService,
  59. IOutputStyleService,
  60. ISettingsSyncService,
  61. IFeedbackService,
  62. IPromptAttachmentTransferService,
  63. IWindowControllerService,
  64. IWindowProjectService,
  65. IWindowRemoteProjectService,
  66. IWindowProjectChangesService,
  67. type WindowProjectDirectoryServices,
  68. type IServiceAccessor,
  69. } from "@zcode/services";
  70. /**
  71. * RemoteServiceAccess — 通过 ChannelClient 自动创建类型安全的服务代理
  72. *
  73. * 新增服务只需在此添加一个 getter。
  74. */
  75. export class RemoteServiceAccess implements IServiceAccessor {
  76. readonly windowProjectDirectory?: WindowProjectDirectoryServices;
  77. readonly fileService: IFileService;
  78. readonly mediaPreviewService: IMediaPreviewService;
  79. readonly gitService: IGitService;
  80. readonly gitCheckpointService: IGitCheckpointService;
  81. readonly systemService: ISystemService;
  82. readonly terminalService: ITerminalService;
  83. readonly settingService: ISettingService;
  84. readonly onboardingRecordService: IOnboardingRecordService;
  85. readonly credentialService: ICredentialService;
  86. readonly broadcastService: IBroadcastService;
  87. readonly zcodeTaskService: IZCodeTaskService;
  88. readonly windowControllerService: IWindowControllerService;
  89. readonly zcodeAgentService: IZCodeAgentService;
  90. readonly zcodeSessionService: IZCodeSessionService;
  91. readonly taskGroupService: ITaskGroupService;
  92. readonly projectService: IProjectService;
  93. readonly managedWorktreeService?: IManagedWorktreeService;
  94. readonly remoteProjectService: IRemoteProjectService;
  95. // cuaPermissionService 在 IServiceAccessor 上是可选(远端/bots host 不提供),但桌面 renderer
  96. // 经 RPC 一定能拿到(main host 始终注册此 descriptor;非 macOS / 未启用时方法返回 available:false)。
  97. readonly cuaPermissionService: ICuaPermissionService;
  98. readonly conversationShareService: IConversationShareService;
  99. readonly botsService: IBotsService;
  100. readonly fileWatcherService: IFileWatcherService;
  101. readonly oauthService: IOAuthService;
  102. readonly providerSettingsService: IProviderSettingsService;
  103. readonly modelSelectionService: IModelSelectionService;
  104. /** Host-only target proxy;不属于 IServiceAccessor,避免向 Renderer 暴露 Secret 写入接口。 */
  105. readonly providerProvisioningTargetService!: IProviderProvisioningTargetService;
  106. readonly sourceServerWorkspaceService: ISourceServerWorkspaceService;
  107. /** 仅 desktop base attachment 注册;其他 scope 必须按可选能力消费。 */
  108. readonly sourceServerManagementService?: ISourceServerManagementService;
  109. /** 仅 desktop base attachment 注册:数据同步页的设备同步状态。 */
  110. readonly deviceSyncStateService?: IDeviceSyncStateService;
  111. readonly providerProvisioningStatusService?: IProviderProvisioningStatusService;
  112. /** 仅 desktop base attachment 注册:扩展(docs/plugins/extension-host.md)。 */
  113. readonly extensionsService?: IExtensionsService;
  114. readonly extensionViewsService?: IExtensionViewsService;
  115. readonly extensionGrantsService?: IExtensionGrantsService;
  116. readonly voiceInputService?: IVoiceInputService;
  117. readonly usageStatsService: IUsageStatsService;
  118. readonly codingPlanSubscriptionService: ICodingPlanSubscriptionService;
  119. readonly clientConfigService: IClientConfigService;
  120. readonly clientScenesService: IClientScenesService;
  121. readonly cloudContentService: ICloudContentService;
  122. readonly marketingTouchService: IMarketingTouchService;
  123. readonly offPeakTaskService: IOffPeakTaskService;
  124. readonly highspeedCardService: IHighspeedCardService;
  125. readonly skillsService: ISkillsService;
  126. readonly skillSyncService: ISkillSyncService;
  127. readonly mcpSyncService: IMcpSyncService;
  128. readonly genUiService: IGenUiService;
  129. readonly pluginUiBridgeService: IPluginUiBridgeService;
  130. readonly pluginUiSamplingService: IPluginUiSamplingService;
  131. readonly pluginUiAppToolsService: IPluginUiAppToolsService;
  132. readonly pluginSyncService: IPluginSyncService;
  133. readonly userConfigSyncService: IUserConfigSyncService;
  134. readonly pluginsService: IPluginsService;
  135. readonly pluginManagementService: IPluginManagementService;
  136. readonly subagentsService: ISubagentsService;
  137. readonly commandsService: ICommandsService;
  138. readonly hooksService: IHooksService;
  139. readonly memoryService: IMemoryService;
  140. readonly outputStyleService: IOutputStyleService;
  141. readonly settingsSyncService: ISettingsSyncService;
  142. readonly feedbackService: IFeedbackService;
  143. readonly promptAttachmentTransferService: IPromptAttachmentTransferService;
  144. constructor(
  145. channelClient: IChannelClient,
  146. options: {
  147. sourceServerManagementService?: boolean;
  148. deviceSyncStateService?: boolean;
  149. extensionsService?: boolean;
  150. managedWorktreeService?: boolean;
  151. windowProjectDirectory?: boolean;
  152. } = {},
  153. ) {
  154. // 远端工作区的 project 必须保留 Source 语义;只有显式能力才创建完整窗口目录组。
  155. if (options.windowProjectDirectory) {
  156. this.windowProjectDirectory = {
  157. projectService: ProxyChannel.toService(
  158. channelClient.getChannel(IWindowProjectService.channelName),
  159. ),
  160. remoteProjectService: ProxyChannel.toService(
  161. channelClient.getChannel(IWindowRemoteProjectService.channelName),
  162. ),
  163. changesService: ProxyChannel.toService(
  164. channelClient.getChannel(IWindowProjectChangesService.channelName),
  165. ),
  166. };
  167. }
  168. this.fileService = ProxyChannel.toService<IFileService>(
  169. channelClient.getChannel(IFileService.channelName),
  170. );
  171. // Host 已注册 media-preview channel,但遗漏 renderer proxy 时,PreviewPane
  172. // 会静默回退到 8 MiB 的 file.readMediaPreview,导致大 MP4 无法打开。
  173. this.mediaPreviewService = ProxyChannel.toService<IMediaPreviewService>(
  174. channelClient.getChannel(IMediaPreviewService.channelName),
  175. );
  176. this.gitService = ProxyChannel.toService<IGitService>(
  177. channelClient.getChannel(IGitService.channelName),
  178. );
  179. this.gitCheckpointService = ProxyChannel.toService<IGitCheckpointService>(
  180. channelClient.getChannel(IGitCheckpointService.channelName),
  181. );
  182. this.systemService = ProxyChannel.toService<ISystemService>(
  183. channelClient.getChannel(ISystemService.channelName),
  184. );
  185. this.terminalService = ProxyChannel.toService<ITerminalService>(
  186. channelClient.getChannel(ITerminalService.channelName),
  187. );
  188. this.settingService = ProxyChannel.toService<ISettingService>(
  189. channelClient.getChannel(ISettingService.channelName),
  190. );
  191. this.onboardingRecordService = ProxyChannel.toService<IOnboardingRecordService>(
  192. channelClient.getChannel(IOnboardingRecordService.channelName),
  193. );
  194. this.credentialService = ProxyChannel.toService<ICredentialService>(
  195. channelClient.getChannel(ICredentialService.channelName),
  196. );
  197. this.broadcastService = ProxyChannel.toService<IBroadcastService>(
  198. channelClient.getChannel(IBroadcastService.channelName),
  199. );
  200. this.zcodeTaskService = ProxyChannel.toService<IZCodeTaskService>(
  201. channelClient.getChannel(IZCodeTaskService.channelName),
  202. );
  203. this.windowControllerService = ProxyChannel.toService<IWindowControllerService>(
  204. channelClient.getChannel(IWindowControllerService.channelName),
  205. );
  206. this.zcodeAgentService = ProxyChannel.toService<IZCodeAgentService>(
  207. channelClient.getChannel(IZCodeAgentService.channelName),
  208. );
  209. this.remoteProjectService = ProxyChannel.toService<IRemoteProjectService>(
  210. channelClient.getChannel(IRemoteProjectService.channelName),
  211. );
  212. this.taskGroupService = ProxyChannel.toService<ITaskGroupService>(
  213. channelClient.getChannel(ITaskGroupService.channelName),
  214. );
  215. this.projectService = ProxyChannel.toService<IProjectService>(
  216. channelClient.getChannel(IProjectService.channelName),
  217. );
  218. // 代理对象不能证明远端注册了频道;只在桌面 base 明确提供能力时创建,避免远程/手机误订阅。
  219. this.managedWorktreeService = options.managedWorktreeService
  220. ? ProxyChannel.toService<IManagedWorktreeService>(
  221. channelClient.getChannel(IManagedWorktreeService.channelName),
  222. )
  223. : undefined;
  224. this.zcodeSessionService = ProxyChannel.toService<IZCodeSessionService>(
  225. channelClient.getChannel(IZCodeSessionService.channelName),
  226. );
  227. this.cuaPermissionService = ProxyChannel.toService<ICuaPermissionService>(
  228. channelClient.getChannel(ICuaPermissionService.channelName),
  229. );
  230. this.conversationShareService = ProxyChannel.toService<IConversationShareService>(
  231. channelClient.getChannel(IConversationShareService.channelName),
  232. );
  233. this.botsService = ProxyChannel.toService<IBotsService>(
  234. channelClient.getChannel(IBotsService.channelName),
  235. );
  236. this.fileWatcherService = ProxyChannel.toService<IFileWatcherService>(
  237. channelClient.getChannel(IFileWatcherService.channelName),
  238. );
  239. this.oauthService = ProxyChannel.toService<IOAuthService>(
  240. channelClient.getChannel(IOAuthService.channelName),
  241. );
  242. this.providerSettingsService = ProxyChannel.toService<IProviderSettingsService>(
  243. channelClient.getChannel(IProviderSettingsService.channelName),
  244. );
  245. this.modelSelectionService = ProxyChannel.toService<IModelSelectionService>(
  246. channelClient.getChannel(IModelSelectionService.channelName),
  247. );
  248. Object.defineProperty(this, "providerProvisioningTargetService", {
  249. value: ProxyChannel.toService<IProviderProvisioningTargetService>(
  250. channelClient.getChannel(IProviderProvisioningTargetService.channelName),
  251. ),
  252. enumerable: false,
  253. });
  254. this.sourceServerWorkspaceService = ProxyChannel.toService<ISourceServerWorkspaceService>(
  255. channelClient.getChannel(ISourceServerWorkspaceService.channelName),
  256. );
  257. // Bug 原因:ProxyChannel 是惰性的,无论服务端是否注册 channel 都能构造出“看似存在”
  258. // 的对象,optional 合同因此在首次调用时才失败。scope 创建时必须显式声明能力。
  259. this.sourceServerManagementService = options.sourceServerManagementService
  260. ? ProxyChannel.toService<ISourceServerManagementService>(
  261. channelClient.getChannel(ISourceServerManagementService.channelName),
  262. )
  263. : undefined;
  264. this.deviceSyncStateService = options.deviceSyncStateService
  265. ? ProxyChannel.toService<IDeviceSyncStateService>(
  266. channelClient.getChannel(IDeviceSyncStateService.channelName),
  267. )
  268. : undefined;
  269. // 与设备同步状态同属数据同步页的本机 Host 服务:同一开关(只在桌面 base 连接打开)。
  270. this.extensionsService = options.extensionsService
  271. ? ProxyChannel.toService<IExtensionsService>(
  272. channelClient.getChannel(IExtensionsService.channelName),
  273. )
  274. : undefined;
  275. // 扩展视图门面与扩展门面同一开关:同一个 Window Host 服务,只是另一个频道(§8)。
  276. this.extensionViewsService = options.extensionsService
  277. ? ProxyChannel.toService<IExtensionViewsService>(
  278. channelClient.getChannel(IExtensionViewsService.channelName),
  279. )
  280. : undefined;
  281. // 扩展授权门面与扩展门面同一开关(§17.7):只在桌面 base 连接打开,不进手机白名单。
  282. this.extensionGrantsService = options.extensionsService
  283. ? ProxyChannel.toService<IExtensionGrantsService>(
  284. channelClient.getChannel(IExtensionGrantsService.channelName),
  285. )
  286. : undefined;
  287. this.providerProvisioningStatusService = options.deviceSyncStateService
  288. ? ProxyChannel.toService<IProviderProvisioningStatusService>(
  289. channelClient.getChannel(IProviderProvisioningStatusService.channelName),
  290. )
  291. : undefined;
  292. // 语音输入只连接本机 Host(docs/chat/composer-voice-input.md):与数据同步同一开关,
  293. // 只在桌面 base 连接打开,Web / 手机远控 / 远程 workspace 连接不创建代理。
  294. this.voiceInputService = options.deviceSyncStateService
  295. ? ProxyChannel.toService<IVoiceInputService>(
  296. channelClient.getChannel(IVoiceInputService.channelName),
  297. )
  298. : undefined;
  299. this.usageStatsService = ProxyChannel.toService<IUsageStatsService>(
  300. channelClient.getChannel(IUsageStatsService.channelName),
  301. );
  302. this.codingPlanSubscriptionService = ProxyChannel.toService<ICodingPlanSubscriptionService>(
  303. channelClient.getChannel(ICodingPlanSubscriptionService.channelName),
  304. );
  305. this.clientConfigService = ProxyChannel.toService<IClientConfigService>(
  306. channelClient.getChannel(IClientConfigService.channelName),
  307. );
  308. this.clientScenesService = ProxyChannel.toService<IClientScenesService>(
  309. channelClient.getChannel(IClientScenesService.channelName),
  310. );
  311. this.cloudContentService = ProxyChannel.toService<ICloudContentService>(
  312. channelClient.getChannel(ICloudContentService.channelName),
  313. );
  314. this.marketingTouchService = ProxyChannel.toService<IMarketingTouchService>(
  315. channelClient.getChannel(IMarketingTouchService.channelName),
  316. );
  317. this.offPeakTaskService = ProxyChannel.toService<IOffPeakTaskService>(
  318. channelClient.getChannel(IOffPeakTaskService.channelName),
  319. );
  320. this.highspeedCardService = ProxyChannel.toService<IHighspeedCardService>(
  321. channelClient.getChannel(IHighspeedCardService.channelName),
  322. );
  323. this.skillsService = ProxyChannel.toService<ISkillsService>(
  324. channelClient.getChannel(ISkillsService.channelName),
  325. );
  326. this.skillSyncService = ProxyChannel.toService<ISkillSyncService>(
  327. channelClient.getChannel(ISkillSyncService.channelName),
  328. );
  329. this.mcpSyncService = ProxyChannel.toService<IMcpSyncService>(
  330. channelClient.getChannel(IMcpSyncService.channelName),
  331. );
  332. this.genUiService = ProxyChannel.toService<IGenUiService>(
  333. channelClient.getChannel(IGenUiService.channelName),
  334. );
  335. this.pluginUiBridgeService = ProxyChannel.toService<IPluginUiBridgeService>(
  336. channelClient.getChannel(IPluginUiBridgeService.channelName),
  337. );
  338. this.pluginUiSamplingService = ProxyChannel.toService<IPluginUiSamplingService>(
  339. channelClient.getChannel(IPluginUiSamplingService.channelName),
  340. );
  341. this.pluginUiAppToolsService = ProxyChannel.toService<IPluginUiAppToolsService>(
  342. channelClient.getChannel(IPluginUiAppToolsService.channelName),
  343. );
  344. this.pluginSyncService = ProxyChannel.toService<IPluginSyncService>(
  345. channelClient.getChannel(IPluginSyncService.channelName),
  346. );
  347. this.userConfigSyncService = ProxyChannel.toService<IUserConfigSyncService>(
  348. channelClient.getChannel(IUserConfigSyncService.channelName),
  349. );
  350. this.pluginsService = ProxyChannel.toService<IPluginsService>(
  351. channelClient.getChannel(IPluginsService.channelName),
  352. );
  353. this.pluginManagementService = ProxyChannel.toService<IPluginManagementService>(
  354. channelClient.getChannel(IPluginManagementService.channelName),
  355. );
  356. this.subagentsService = ProxyChannel.toService<ISubagentsService>(
  357. channelClient.getChannel(ISubagentsService.channelName),
  358. );
  359. this.commandsService = ProxyChannel.toService<ICommandsService>(
  360. channelClient.getChannel(ICommandsService.channelName),
  361. );
  362. this.hooksService = ProxyChannel.toService<IHooksService>(
  363. channelClient.getChannel(IHooksService.channelName),
  364. );
  365. this.memoryService = ProxyChannel.toService<IMemoryService>(
  366. channelClient.getChannel(IMemoryService.channelName),
  367. );
  368. this.outputStyleService = ProxyChannel.toService<IOutputStyleService>(
  369. channelClient.getChannel(IOutputStyleService.channelName),
  370. );
  371. this.settingsSyncService = ProxyChannel.toService<ISettingsSyncService>(
  372. channelClient.getChannel(ISettingsSyncService.channelName),
  373. );
  374. this.feedbackService = ProxyChannel.toService<IFeedbackService>(
  375. channelClient.getChannel(IFeedbackService.channelName),
  376. );
  377. this.promptAttachmentTransferService = ProxyChannel.toService<IPromptAttachmentTransferService>(
  378. channelClient.getChannel(IPromptAttachmentTransferService.channelName),
  379. );
  380. }
  381. }