* Starts the CLI server and establishes a connection. * * If connecting to an external server (via cliUrl), only establishes the connection. * Otherwise, spawns the CLI server process and then connects. * * This method is called automatically the first time you create or res
()
| 785 | * ``` |
| 786 | */ |
| 787 | async start(): Promise<void> { |
| 788 | if (this.state === "connected") { |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | this.state = "connecting"; |
| 793 | |
| 794 | try { |
| 795 | // Only start CLI server process if not connecting to external server |
| 796 | if (!this.isExternalServer) { |
| 797 | await this.startCLIServer(); |
| 798 | } |
| 799 | |
| 800 | // Connect to the server |
| 801 | await this.connectToServer(); |
| 802 | |
| 803 | // Verify protocol version compatibility |
| 804 | await this.verifyProtocolVersion(); |
| 805 | |
| 806 | // If a session filesystem provider was configured, register it |
| 807 | if (this.sessionFsConfig) { |
| 808 | await this.connection!.sendRequest("sessionFs.setProvider", { |
| 809 | initialCwd: this.sessionFsConfig.initialCwd, |
| 810 | sessionStatePath: this.sessionFsConfig.sessionStatePath, |
| 811 | conventions: this.sessionFsConfig.conventions, |
| 812 | capabilities: this.sessionFsConfig.capabilities, |
| 813 | }); |
| 814 | } |
| 815 | |
| 816 | // If a request handler was configured, register it. The runtime |
| 817 | // will then route outbound model HTTP requests through the |
| 818 | // registered handler for the duration of each session. |
| 819 | if (this.requestHandler) { |
| 820 | await this.connection!.sendRequest("llmInference.setProvider", {}); |
| 821 | } |
| 822 | |
| 823 | this.state = "connected"; |
| 824 | } catch (error) { |
| 825 | this.state = "error"; |
| 826 | throw error; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Stops the CLI server and closes all active sessions. |