Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ export class ChatDebugComponent {
this.ref().isLoading(); // track
const el = this.scrollContainer()?.nativeElement;
if (el) {
setTimeout(() => el.scrollTop = el.scrollHeight, 0);
const isNearBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 150;
if (isNearBottom) {
requestAnimationFrame(() => {
el.scrollTo({ top: el.scrollHeight, behavior: 'smooth' });
});
}
}
});
}
Expand Down
11 changes: 9 additions & 2 deletions libs/chat/src/lib/compositions/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,20 @@ export class ChatComponent {
private readonly messageCount = computed(() => this.ref().messages().length);

constructor() {
// Auto-scroll to bottom when new messages arrive or loading state changes
// Auto-scroll to bottom when new messages arrive.
// Only scrolls if user is already near the bottom (within 150px),
// so reading earlier messages isn't interrupted.
effect(() => {
this.messageCount(); // track
this.ref().isLoading(); // track
const el = this.scrollContainer()?.nativeElement;
if (el) {
setTimeout(() => el.scrollTop = el.scrollHeight, 0);
const isNearBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 150;
if (isNearBottom) {
requestAnimationFrame(() => {
el.scrollTo({ top: el.scrollHeight, behavior: 'smooth' });
});
}
}
});
}
Expand Down
10 changes: 9 additions & 1 deletion libs/stream-resource/src/lib/internals/stream-manager.bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ export interface StreamManagerBridge {
export function createStreamManagerBridge<T, ResolvedBag extends BagTemplate = BagTemplate>(
{ options, subjects, threadId$, destroy$ }: StreamManagerBridgeOptions<T, ResolvedBag>
): StreamManagerBridge {
// Intercept onThreadId to update currentThreadId when the transport
// auto-creates a thread. Without this, each submit() creates a new thread
// because currentThreadId stays null.
const userOnThreadId = options.onThreadId;
const wrappedOnThreadId = (id: string) => {
currentThreadId = id;
userOnThreadId?.(id);
};
const transport: StreamResourceTransport =
options.transport ?? new FetchStreamTransport(options.apiUrl, options.onThreadId);
options.transport ?? new FetchStreamTransport(options.apiUrl, wrappedOnThreadId);

let currentThreadId: string | null = null;
let lastPayload: unknown = null;
Expand Down