MCPcopy Index your code
hub / github.com/ZToolsCenter/ZTools / registerTool

Method registerTool

src/main/api/plugin/tools.ts:226–252  ·  view source on GitHub ↗

* 记录插件工具注册结果,并唤醒等待该工具可用的调用方。

(webContents: WebContents, rawToolName: string)

Source from the content-addressed store, hash-verified

224 * 记录插件工具注册结果,并唤醒等待该工具可用的调用方。
225 */
226 private registerTool(webContents: WebContents, rawToolName: string): void {
227 const toolName = typeof rawToolName === 'string' ? rawToolName.trim() : ''
228 if (!toolName) {
229 throw new Error('工具名称不能为空')
230 }
231
232 const pluginInfo = this.pluginManager?.getPluginInfoByWebContents(webContents)
233 if (!pluginInfo) {
234 throw new Error('无法获取插件信息')
235 }
236
237 const declaredTools = this.getDeclaredToolsByPath(pluginInfo.path)
238 if (!declaredTools[toolName]) {
239 throw new Error(`工具 "${toolName}" 未在 plugin.json 中声明`)
240 }
241
242 let tools = this.registeredTools.get(webContents.id)
243 if (!tools) {
244 tools = new Set<string>()
245 this.registeredTools.set(webContents.id, tools)
246 webContents.once('destroyed', () => {
247 this.registeredTools.delete(webContents.id)
248 })
249 }
250 tools.add(toolName)
251 this.resolveWaiters(webContents.id, toolName)
252 }
253
254 /**
255 * 等待 preload 中的 registerTool 完成,避免刚预加载时立即调用失败。

Callers 1

setupIPCMethod · 0.95

Calls 4

resolveWaitersMethod · 0.95
getMethod · 0.65

Tested by

no test coverage detected