@frankieai/react 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frankieai/react",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Embeddable, themeable chat widget for the Frankie onboarding agent — shadcn/ui components, works with or without Tailwind in the host app.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -52,17 +52,35 @@ export function ChatPanel({
52
52
  const theme = useFrankieTheme();
53
53
  const { messages, busy, restoring, error, send, stop, reset } = useFrankieChat();
54
54
  const scrollRef = useRef<HTMLDivElement>(null);
55
+ // Tracks whether the reader was at the bottom just before this render, not
56
+ // where the bottom ends up after it. Measuring post-render (against the
57
+ // already-grown scrollHeight) breaks as soon as one update — a sent message
58
+ // plus the reply's opening burst of text, say — adds more than the near-
59
+ // bottom threshold in a single paint: the check then reads "scrolled away"
60
+ // even though the reader never touched the scrollbar, and every later
61
+ // delta stops following. A live scroll listener captures the real, current
62
+ // position instead of re-deriving a stale one from updated content.
63
+ const atBottomRef = useRef(true);
55
64
 
56
65
  const features = config?.features;
57
66
  const pickerModels = features?.modelPicker.enabled ? features.modelPicker.models : [];
58
67
  const [model, setModel] = useState<string | undefined>(undefined);
59
68
 
60
- // Follow the stream, but only while the reader is already near the bottom.
61
69
  useEffect(() => {
62
70
  const node = scrollRef.current;
63
71
  if (!node) return;
64
- const distanceFromBottom = node.scrollHeight - node.scrollTop - node.clientHeight;
65
- if (distanceFromBottom < 160) node.scrollTop = node.scrollHeight;
72
+ const onScroll = () => {
73
+ atBottomRef.current = node.scrollHeight - node.scrollTop - node.clientHeight < 160;
74
+ };
75
+ node.addEventListener("scroll", onScroll, { passive: true });
76
+ return () => node.removeEventListener("scroll", onScroll);
77
+ }, []);
78
+
79
+ // Follow the stream, but only while the reader is already near the bottom.
80
+ useEffect(() => {
81
+ const node = scrollRef.current;
82
+ if (!node || !atBottomRef.current) return;
83
+ node.scrollTop = node.scrollHeight;
66
84
  }, [messages]);
67
85
 
68
86
  const name = config?.branding.name ?? "Assistant";
@@ -227,6 +227,40 @@ describe("ChatPanel", () => {
227
227
  expect(screen.queryByText("1 skill loaded")).toBeNull();
228
228
  cleanup();
229
229
  });
230
+ it("keeps following new messages even when one update grows the transcript past the near-bottom threshold", async () => {
231
+ cleanup();
232
+ const fetchImpl = vi.fn(async (url: string | URL | Request) =>
233
+ String(url).endsWith("/v1/session")
234
+ ? Response.json({ sessionId: "scroll", sessionToken: "fixture", expiresAt: new Date(Date.now() + 600_000).toISOString(), user: { id: "one", anonymous: true } })
235
+ : new Response(sseStream([{ type: "text_delta", text: "A substantially longer reply than the one before it, to grow the transcript by more than the near-bottom threshold in a single update." }, { type: "turn_end", stopReason: "end_turn" }])),
236
+ );
237
+ const { container } = render(<FrankieProvider publishableKey="pk_scroll_fixture" config={config} fetchImpl={fetchImpl} persist={false}><ChatPanel fill /></FrankieProvider>);
238
+ const scroller = container.querySelector(".frankie-transcript") as HTMLElement;
239
+ let scrollTop = 0;
240
+ // jsdom has no layout engine, so scrollHeight/clientHeight are always 0.
241
+ // Tie scrollHeight to the actual rendered text so it grows the way a real
242
+ // browser's would as messages stream in, without hand-stepping it.
243
+ Object.defineProperty(scroller, "clientHeight", { configurable: true, get: () => 400 });
244
+ Object.defineProperty(scroller, "scrollHeight", { configurable: true, get: () => (scroller.textContent?.length ?? 0) * 50 });
245
+ Object.defineProperty(scroller, "scrollTop", { configurable: true, get: () => scrollTop, set: (value: number) => { scrollTop = value; } });
246
+
247
+ await waitFor(() => expect((screen.getByLabelText("Message") as HTMLTextAreaElement).disabled).toBe(false));
248
+ fireEvent.change(screen.getByLabelText("Message"), { target: { value: "First question" } });
249
+ fireEvent.click(screen.getByRole("button", { name: "Send" }));
250
+ await waitFor(() => expect(scrollTop).toBe(scroller.scrollHeight));
251
+
252
+ // The reader never touched the scrollbar between turns — they were left
253
+ // exactly at the bottom by the first turn. A second turn whose reply
254
+ // alone adds far more than 160px of content in one commit must still be
255
+ // followed: recomputing "distance from bottom" from the post-update DOM
256
+ // (the pre-fix approach) misreads that jump as the reader having
257
+ // scrolled away, and stops following for the rest of the turn.
258
+ fireEvent.change(screen.getByLabelText("Message"), { target: { value: "Second question" } });
259
+ fireEvent.click(screen.getByRole("button", { name: "Send" }));
260
+ await screen.findByText(/A substantially longer reply/);
261
+ await waitFor(() => expect(scrollTop).toBe(scroller.scrollHeight));
262
+ cleanup();
263
+ });
230
264
  it("refreshes the context count and revisions from each server turn", async () => {
231
265
  cleanup();
232
266
  let turn = 0;