((resolve) => {
- this.agent.on("didChangeStatus", (status) => {
- if (status === "ready") {
- resolve();
- }
- });
- });
+
+ if (this.isReady) {
+ await this.renderChatPage();
+ } else {
+ webviewView.webview.html = this.getWelcomeContent();
}
- const serverInfo = await this.agent.fetchServerInfo();
- webviewView.webview.html = this.getWebviewContent(serverInfo);
- this.agent.on("didUpdateServerInfo", (serverInfo: ServerInfo) => {
- webviewView.webview.html = this.getWebviewContent(serverInfo);
- });
this.client = createClient(webviewView, {
navigate: async (context: Context) => {
@@ -50,18 +43,27 @@ export class ChatViewProvider implements WebviewViewProvider {
},
});
+ this.agent.on("didChangeStatus", async (status) => {
+ if (status === "ready" && !this.isReady) {
+ this.isReady = true;
+ await this.renderChatPage();
+ }
+ });
+
+ this.agent.on("didUpdateServerInfo", async (serverInfo: ServerInfo) => {
+ await this.renderChatPage(serverInfo);
+ });
+
+ // The event will not be triggered during the initial rendering.
+ webviewView.onDidChangeVisibility(async () => {
+ if (webviewView.visible) {
+ await this.initChatPage();
+ }
+ });
+
webviewView.webview.onDidReceiveMessage(async (message) => {
if (message.action === "rendered") {
- this.webview?.webview.postMessage({ action: "sync-theme" });
- this.pendingMessages.forEach((message) => this.client?.sendMessage(message));
- const serverInfo = await this.agent.fetchServerInfo();
- if (serverInfo.config.token) {
- this.client?.init({
- fetcherOptions: {
- authorization: serverInfo.config.token,
- },
- });
- }
+ await this.initChatPage();
}
});
@@ -72,6 +74,15 @@ export class ChatViewProvider implements WebviewViewProvider {
});
}
+ private async renderChatPage(serverInfo?: ServerInfo) {
+ if (!serverInfo) {
+ serverInfo = await this.agent.fetchServerInfo();
+ }
+ if (this.webview) {
+ this.webview.webview.html = this.getWebviewContent(serverInfo);
+ }
+ }
+
private isChatPanelAvailable(serverInfo: ServerInfo): boolean {
if (!serverInfo.health || !serverInfo.health["webserver"] || !serverInfo.health["chat_model"]) {
return false;
@@ -94,24 +105,28 @@ export class ChatViewProvider implements WebviewViewProvider {
return true;
}
+ private async initChatPage() {
+ this.webview?.webview.postMessage({ action: "sync-theme" });
+ this.pendingMessages.forEach((message) => this.client?.sendMessage(message));
+ const serverInfo = await this.agent.fetchServerInfo();
+ if (serverInfo.config.token) {
+ this.client?.init({
+ fetcherOptions: {
+ authorization: serverInfo.config.token,
+ },
+ });
+ }
+ }
+
private getWebviewContent(serverInfo: ServerInfo) {
if (!this.isChatPanelAvailable(serverInfo)) {
- return `
-
-
-
-
-
-
- Tabby is not available
-
- - Please update to the latest version of the Tabby server.
- - You also need to launch the server with the chat model enabled; for example, use
--chat-model Mistral-7B
.
-
-
-
- `;
+ return this.getStaticContent(`
+ Tabby is not available
+ Please update to the latest version of the Tabby server.
+ You also need to launch the server with the chat model enabled; for example, use --chat-model Mistral-7B
.
+ `);
}
+
const endpoint = serverInfo.config.endpoint;
const styleUri = this.webview?.webview.asWebviewUri(
Uri.joinPath(this.context.extensionUri, "assets", "chat-panel.css"),
@@ -123,7 +138,6 @@ export class ChatViewProvider implements WebviewViewProvider {
- Tabby