* 在插件上下文中执行已注册的工具处理器。
(
webContents: WebContents,
toolName: string,
input: unknown
)
| 195 | * 在插件上下文中执行已注册的工具处理器。 |
| 196 | */ |
| 197 | public async executeRegisteredTool( |
| 198 | webContents: WebContents, |
| 199 | toolName: string, |
| 200 | input: unknown |
| 201 | ): Promise<unknown> { |
| 202 | const declaredTools = this.getDeclaredToolsByWebContents(webContents) |
| 203 | if (!declaredTools?.[toolName]) { |
| 204 | throw new Error(`工具 "${toolName}" 未在 plugin.json 中声明`) |
| 205 | } |
| 206 | if (!this.isToolRegistered(webContents, toolName)) { |
| 207 | throw new Error(`工具 "${toolName}" 尚未通过 ztools.registerTool 注册`) |
| 208 | } |
| 209 | |
| 210 | return await webContents.executeJavaScript(` |
| 211 | (async () => { |
| 212 | if (!window.ztools || typeof window.ztools.__invokeRegisteredTool !== 'function') { |
| 213 | throw new Error('插件运行时缺少工具调用入口') |
| 214 | } |
| 215 | return await window.ztools.__invokeRegisteredTool( |
| 216 | ${JSON.stringify(toolName)}, |
| 217 | ${JSON.stringify(input ?? {})} |
| 218 | ) |
| 219 | })() |
| 220 | `) |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * 记录插件工具注册结果,并唤醒等待该工具可用的调用方。 |
no test coverage detected