Skip to content

Commit af0aa8c

Browse files
committed
fix: optimize message height calculation and remove unnecessary waiting logic
1 parent 22ad6ee commit af0aa8c

2 files changed

Lines changed: 30 additions & 32 deletions

File tree

custom/conversation_area/ConversationArea.vue

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,14 @@ const agentTransitions = useAgentTransitions();
9393
const showScrollContainer = ref(true);
9494
const chatContainerRef = ref<HTMLElement | null>(null);
9595
const messagesRefs = ref<Array<HTMLElement | null>>([]);
96-
const useWaitingForHeight = ref(false);
97-
9896
/*
9997
* Whenever user sends a message, it adds a bottom spacer, that takes the remaining height
10098
* without last user and last agent message.
10199
*
102100
* On send message, happens the following logic:
103101
* 1) showBottomSpacer is set to true
104-
* 2) useWaitingForHeight is set to true and in 1000s set back to false
105-
* Why do we do this?
106-
* - When we want to read height of last user message, incremark shows text with some small delay
107-
* - so when we read height of last user message, we actully getting height of the box without text ~18px
108-
* - so for the initial period while useWaitingForHeight is true, we are waiting for real height to be bigger than 18px
109-
* - and then we can read in normally, until new message is sent, then we need to wait again
110-
* 3) updateSpacerHeight is called, which calculates the height for spacer based on scroll container height and messages height
111-
* 4) Spacer moves text up
102+
* 2) updateSpacerHeight is called, which calculates the height for spacer based on scroll container height and messages height
103+
* 3) Spacer moves text up
112104
*/
113105
const showBottomSpacer = ref(false);
114106
const spacerHeight = ref(0);
@@ -197,20 +189,6 @@ function getScrollClientHeight() {
197189
return scrollContainer.value?.container.scrollEl.clientHeight ?? scrollContainer.value?.scrollParams.clientHeight ?? 0;
198190
}
199191
200-
async function waitForRealHeight(role: 'user' | 'assistant'): Promise<number> {
201-
const realHeightWeCanApprove = role === 'user' ? EMPTY_MESSAGE_HEIGHT : 0;
202-
return new Promise((resolve) => {
203-
const interval = setInterval(() => {
204-
const height = role === 'user' ? getHeightOfLastUserMessage() : getHeightOfLastAgentMessage();
205-
206-
if (height > realHeightWeCanApprove) {
207-
clearInterval(interval);
208-
resolve(height);
209-
}
210-
}, 50);
211-
});
212-
}
213-
214192
async function updateSpacerHeight() {
215193
if (!showBottomSpacer.value) {
216194
return;
@@ -222,8 +200,8 @@ async function updateSpacerHeight() {
222200
return;
223201
}
224202
225-
const lastUserMessageHeight = useWaitingForHeight.value ? await waitForRealHeight('user') : getHeightOfLastUserMessage();
226-
const lastAgentMessageHeight = useWaitingForHeight.value ? await waitForRealHeight('assistant') : getHeightOfLastAgentMessage();
203+
const lastUserMessageHeight = getHeightOfLastUserMessage();
204+
const lastAgentMessageHeight = getHeightOfLastAgentMessage();
227205
228206
spacerHeight.value = Math.max(0, clientHeight - (lastUserMessageHeight + MASK_HEIGHT + lastAgentMessageHeight));
229207
}
@@ -245,6 +223,14 @@ function scheduleSpacerHeightUpdate() {
245223
spacerUpdateQueued = false;
246224
pendingSpacerUpdate = updateSpacerHeight().finally(() => {
247225
pendingSpacerUpdate = null;
226+
227+
// Auto-scroll to bottom if response generation is in progress
228+
if (agentStore.isResponseInProgress) {
229+
nextTick(() => {
230+
scrollContainer.value?.scrollToBottom();
231+
});
232+
}
233+
248234
if (spacerUpdateQueued) {
249235
scheduleSpacerHeightUpdate();
250236
}
@@ -298,10 +284,6 @@ async function handleSendMessage() {
298284
299285
if (clientHeight) {
300286
showBottomSpacer.value = true;
301-
useWaitingForHeight.value = true;
302-
setTimeout(() => {
303-
useWaitingForHeight.value = false;
304-
}, 1000);
305287
await updateSpacerHeight();
306288
await nextTick();
307289
scrollContainer.value?.scrollToBottom();

index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
3535
options: PluginOptions;
3636
agentSystemPromptPromise: Promise<string>;
3737
pluginsScope: "resource" | "global" = "global";
38+
private agentSystemPrompt: string | null = null;
3839
private checkpointer: BaseCheckpointSaver | null = null;
3940
private sessionStore: AgentSessionStore;
4041
private agentTurnService: AgentTurnService;
@@ -92,7 +93,18 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
9293
new AgentModelFactory(this.options.maxTokens ?? 1000),
9394
new TurnPromptBuilder({
9495
getAdminforth: () => this.adminforth,
95-
getAgentSystemPrompt: () => this.agentSystemPromptPromise,
96+
getAgentSystemPrompt: async () => {
97+
if (!this.agentSystemPrompt) {
98+
const systemPrompt = await buildAgentSystemPrompt(
99+
this.adminforth,
100+
this.getInternalAgentResourceIds(),
101+
).catch((err) => {
102+
return DEFAULT_AGENT_SYSTEM_PROMPT;
103+
});
104+
this.agentSystemPrompt = appendCustomSystemPrompt(systemPrompt, this.options.systemPrompt);
105+
}
106+
return this.agentSystemPrompt;
107+
},
96108
}),
97109
runtime,
98110
new TurnStreamConsumer(),
@@ -162,7 +174,11 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
162174
adminforth,
163175
this.getInternalAgentResourceIds(),
164176
)
165-
.then((systemPrompt) => appendCustomSystemPrompt(systemPrompt, this.options.systemPrompt));
177+
.then((systemPrompt) => {
178+
const finalPrompt = appendCustomSystemPrompt(systemPrompt, this.options.systemPrompt);
179+
this.agentSystemPrompt = finalPrompt;
180+
return finalPrompt;
181+
});
166182
}
167183

168184
instanceUniqueRepresentation(pluginOptions: any) : string {

0 commit comments

Comments
 (0)