Skip to content

Commit f1cecf5

Browse files
authored
feat: long messages should automatically scroll so the bottom is visible (#162)
1 parent 55f00d0 commit f1cecf5

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

media/chat.css

+20
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@
154154
text-align: center;
155155
}
156156

157+
/* Error states inside chat bubbles */
158+
.sidebar__chat-assistant--chat-bubble-error
159+
.sidebar__chat-assistant--chat-bubble-content-assistant {
160+
background: var(--vscode-inputValidation-errorBackground);
161+
}
162+
163+
.sidebar__chat-assistant--chat-bubble-error
164+
.sidebar__chat-assistant--chat-bubble-text {
165+
color: var(--vscode-inputValidation-errorForeground);
166+
}
167+
168+
.sidebar__chat-assistant--chat-bubble-error
169+
.sidebar__chat-assistant--chat-avatar-container {
170+
background: var(--vscode-inputValidation-errorBackground);
171+
}
172+
173+
.sidebar__chat-assistant--chat-bubble-error svg {
174+
fill: var(--vscode-sideBar-foreground);
175+
}
176+
157177
.sidebar__chat-assistant--cursor {
158178
display: inline-block;
159179
animation: cursor-blink 1.5s steps(2) infinite;

src/webview/chat.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@
7676
function sendRecipeRequest(message) {
7777
// Ensure we don't add on to the previous message
7878
assistantMessageFinished();
79-
addAssistantMessageToUI("Executing Recipe: " + message);
79+
addAssistantMessageToUI({
80+
textContent: "Executing Recipe: " + message,
81+
outcome: "success",
82+
});
8083
// Ensure new responses don't get added on to this one
8184
assistantMessageFinished();
8285
sendRequestToExtension(message);
@@ -103,6 +106,7 @@
103106
);
104107
userMessageElement.innerHTML = templateMessage;
105108
chatContainer.append(userMessageElement);
109+
userMessageElement.scrollIntoView();
106110
assistantMessageFinished();
107111
}
108112

@@ -116,13 +120,11 @@
116120
const addMessageToCurrentAssistantMessage = () => {
117121
let assistantMessageSpan = document.createElement("span");
118122
assistantMessageSpan.textContent = message.textContent;
119-
if (message.outcome === "error") {
120-
assistantMessageSpan.style.color = "red";
121-
}
122123
currentAssistantMessage.append(assistantMessageSpan);
124+
assistantMessageSpan.scrollIntoView();
123125
};
124126

125-
if (currentAssistantMessage != null) {
127+
if (currentAssistantMessage != null && message.outcome !== "error") {
126128
addMessageToCurrentAssistantMessage();
127129
} else {
128130
const templateContents = `
@@ -139,6 +141,12 @@
139141
assistantMessageElement.classList.add(
140142
"sidebar__chat-assistant--chat-bubble-agent"
141143
);
144+
145+
if (message.outcome === "error") {
146+
assistantMessageElement.classList.add(
147+
"sidebar__chat-assistant--chat-bubble-error"
148+
);
149+
}
142150
assistantMessageElement.innerHTML = templateContents;
143151
chatContainer.append(assistantMessageElement);
144152
currentAssistantMessage = assistantMessageElement.querySelector(
@@ -155,6 +163,7 @@
155163
}
156164
thinkingMessage = thinkingMessageElement;
157165
chatContainer.append(thinkingMessage);
166+
thinkingMessage.scrollIntoView();
158167
}
159168

160169
// Enable/Disable send button depending on whether text area is empty
@@ -164,9 +173,10 @@
164173
} else {
165174
sendButton.classList.add("sidebar__textarea-send-button--disabled");
166175
}
176+
adjustTextareaHeight(messageInput);
167177
}
168178

169-
// Check for disable/enable send button
179+
// Check for disable/enable send button and sizing
170180
messageInput.addEventListener("input", checkTextarea);
171181

172182
// Check to see if we need to disable send button on backspace
@@ -183,4 +193,15 @@
183193
sendUserMessage();
184194
}
185195
});
196+
197+
const adjustTextareaHeight = (textarea) => {
198+
// Reset height to auto to get the actual scroll height, and set it to that value
199+
textarea.style.height = "auto";
200+
textarea.style.height = textarea.scrollHeight + "px";
201+
202+
// Check if scrolling occurs after setting the new height
203+
if (textarea.clientHeight < textarea.scrollHeight) {
204+
textarea.style.height = textarea.scrollHeight + "px";
205+
}
206+
};
186207
})();

0 commit comments

Comments
 (0)