* Executes a command handler and sends the result back via RPC. * @internal
(
requestId: string,
commandName: string,
command: string,
args: string
)
| 717 | * @internal |
| 718 | */ |
| 719 | private async _executeCommandAndRespond( |
| 720 | requestId: string, |
| 721 | commandName: string, |
| 722 | command: string, |
| 723 | args: string |
| 724 | ): Promise<void> { |
| 725 | const handler = this.commandHandlers.get(commandName); |
| 726 | if (!handler) { |
| 727 | try { |
| 728 | await this.rpc.commands.handlePendingCommand({ |
| 729 | requestId, |
| 730 | error: `Unknown command: ${commandName}`, |
| 731 | }); |
| 732 | } catch (rpcError) { |
| 733 | if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) { |
| 734 | throw rpcError; |
| 735 | } |
| 736 | } |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | try { |
| 741 | await handler({ sessionId: this.sessionId, command, commandName, args }); |
| 742 | if (this.disconnected) { |
| 743 | return; |
| 744 | } |
| 745 | await this.rpc.commands.handlePendingCommand({ requestId }); |
| 746 | } catch (error) { |
| 747 | if (this.disconnected) { |
| 748 | return; |
| 749 | } |
| 750 | const message = error instanceof Error ? error.message : String(error); |
| 751 | try { |
| 752 | await this.rpc.commands.handlePendingCommand({ requestId, error: message }); |
| 753 | } catch (rpcError) { |
| 754 | if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) { |
| 755 | throw rpcError; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Registers custom tool handlers for this session. |
no test coverage detected