Skip to content

Conversation

@AdityaHegde
Copy link
Collaborator

@AdityaHegde AdityaHegde commented Dec 30, 2025

In chat history we autoscroll to the bottom if the user was already at the bottom. We do this by checking if a new block has come in. But if the block type changes then the scroll height could change, especially when a long large assistant message block replaces the "working" block.

Adding an additional check for block type change to fix this.

This simulates a conversation by modifying query cache of an existing conversaiont. Call this on mount somewhere. Running on a rill developer file edit chat will give the best results.

import type { Conversation } from "@rilldata/web-common/features/chat/core/conversation.ts";
import { asyncWait, waitUntil } from "@rilldata/web-common/lib/waitUtils.ts";
import { get } from "svelte/store";
import {
  getRuntimeServiceGetConversationQueryKey,
  type V1Message,
} from "@rilldata/web-common/runtime-client";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store.ts";
import { queryClient } from "@rilldata/web-common/lib/svelte-query/globalQueryClient.ts";

export async function simulateConversation(
  conversation: Conversation,
  beforeStart: () => void,
) {
  const convQuery = conversation.getConversationQuery();
  await waitUntil(() => !get(convQuery).isPending);

  const resp = get(convQuery).data;
  if (!resp || !resp.messages) return;

  const queryKey = getRuntimeServiceGetConversationQueryKey(
    get(runtime).instanceId,
    conversation.conversationId,
  );
  const messages: V1Message[] = [];
  const updateQueryCache = (message: V1Message) => {
    messages.push({
      ...message,
      createdOn: new Date().toISOString(),
      updatedOn: new Date().toISOString(),
    });
    queryClient.setQueryData(queryKey, {
      conversation: resp.conversation,
      messages,
    });
  };

  beforeStart();
  conversation.isStreaming.set(true);

  updateQueryCache(resp.messages[0]);
  for (let i = 1; i < resp.messages.length; i++) {
    const prev = new Date(resp.messages[i - 1].createdOn!);
    const now = new Date(resp.messages[i].updatedOn!);
    // Dividing by 5 to speed things up.
    await asyncWait((now.getTime() - prev.getTime()) / 5);

    updateQueryCache(resp.messages[i]);
  }

  await asyncWait(500); 
  conversation.isStreaming.set(false);
}

Checklist:

  • Covered by tests
  • Ran it and it works as intended
  • Reviewed the diff before requesting a review
  • Checked for unhandled edge cases
  • Linked the issues it closes
  • Checked if the docs need to be updated. If so, create a separate Linear DOCS issue
  • Intend to cherry-pick into the release branch
  • I'm proud of this work!

@AdityaHegde AdityaHegde force-pushed the fix/auto-scroll-for-assistant-message branch from 400abd5 to 61a882b Compare December 30, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants