Repository: TabbyML/tabby
File: clients/tabby-agent/src/http/tabbyApiClient.ts (TypeScript)

Code before:
    this.userAgentString = this.buildUserAgentString(clientInfo);
    this.api = this.createApiClient();
    this.connect(); // no await

    this.configurations.on("updated", (config: ConfigData, oldConfig: ConfigData) => {
      const isServerConnectionChanged = !(
        deepEqual(config.server, oldConfig.server) && deepEqual(config.proxy, oldConfig.proxy)
      );
      if (isServerConnectionChanged) {
        this.logger.debug("Server configurations updated, reconnecting...");
        this.reset();
        this.connect(); // no await
      }
    });

    const reconnectInterval = 1000 * 30; // 30s
    this.reconnectTimer = setInterval(async () => {
      if (this.status === "noConnection" || this.status === "unauthorized") {
        this.logger.debug("Trying to reconnect...");
        await this.connect();
      }
    }, reconnectInterval);
    setTimeout(() => {
      const heartBeatInterval = 1000 * 60; // 1m
      this.heartbeatTimer = setInterval(async () => {
        if (this.status !== "noConnection" && this.status !== "unauthorized") {
          this.logger.debug("Heartbeat...");
          await this.healthCheck({ background: true });
        }
      }, heartBeatInterval);
    }, 10000);
  }

  async shutdown() {
    if (this.reconnectTimer) {
      clearInterval(this.reconnectTimer);
    }
    if (this.heartbeatTimer) {
      clearInterval(this.heartbeatTimer);
    }
  }

  private reset() {
    this.updateStatus("noConnection");
    this.updateCompletionResponseIssue(undefined);
    this.connectionErrorMessage = undefined;
    this.serverHealth = undefined;
    this.completionRequestStats.reset();
    this.api = this.createApiClient();
  }


Review comment (suggestion):
```suggestion
        if (this.status === "ready") {
```

Code after:
    this.userAgentString = this.buildUserAgentString(clientInfo);
    this.api = this.createApiClient();
    this.connect(); // no await

    this.configurations.on("updated", (config: ConfigData, oldConfig: ConfigData) => {
      const isServerConnectionChanged = !(
        deepEqual(config.server, oldConfig.server) && deepEqual(config.proxy, oldConfig.proxy)
      );
      if (isServerConnectionChanged) {
        this.logger.debug("Server configurations updated, reconnecting...");
        this.reset();
        this.connect(); // no await
      }
    });

    const reconnectInterval = 1000 * 30; // 30s
    this.reconnectTimer = setInterval(async () => {
      if (this.status === "noConnection" || this.status === "unauthorized") {
        this.logger.debug("Trying to reconnect...");
        await this.connect();
      }
    }, reconnectInterval);

    const heartBeatInterval = 1000 * 60; // 1m
    this.heartbeatTimer = setInterval(async () => {
      if (this.status === "ready") {
        this.logger.debug("Heartbeat...");
        await this.healthCheck({ background: true });
      }
    }, heartBeatInterval);
  }

  async shutdown() {
    if (this.reconnectTimer) {
      clearInterval(this.reconnectTimer);
    }
    if (this.heartbeatTimer) {
      clearInterval(this.heartbeatTimer);
    }
  }

  private reset() {
    this.updateStatus("noConnection");
    this.updateCompletionResponseIssue(undefined);
    this.connectionErrorMessage = undefined;
    this.serverHealth = undefined;
    this.completionRequestStats.reset();
    this.api = this.createApiClient();
  }

  private buildUserAgentString(clientInfo: ClientInfo | undefined): string {