Alleinzellgaenger commited on
Commit
d82d893
·
1 Parent(s): f444dc0

Add streaming

Browse files
backend/app.py CHANGED
@@ -1,13 +1,14 @@
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.staticfiles import StaticFiles
4
- from fastapi.responses import FileResponse
5
  import os
6
  import tempfile
7
  from dotenv import load_dotenv
8
  from pydantic import BaseModel
9
  from typing import Optional, List
10
  import anthropic
 
11
 
12
  # Load environment variables
13
  load_dotenv()
@@ -315,6 +316,269 @@ async def upload_pdf(file: UploadFile = File(...)):
315
  print(f"❌ Error uploading PDF: {e}")
316
  raise HTTPException(status_code=500, detail=f"PDF upload error: {str(e)}")
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  # Mount static files for production deployment
319
  frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend")
320
  assets_path = os.path.join(frontend_path, "assets")
 
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.staticfiles import StaticFiles
4
+ from fastapi.responses import FileResponse, StreamingResponse
5
  import os
6
  import tempfile
7
  from dotenv import load_dotenv
8
  from pydantic import BaseModel
9
  from typing import Optional, List
10
  import anthropic
11
+ import json
12
 
13
  # Load environment variables
14
  load_dotenv()
 
316
  print(f"❌ Error uploading PDF: {e}")
317
  raise HTTPException(status_code=500, detail=f"PDF upload error: {str(e)}")
318
 
319
+ @app.post("/api/chat/stream")
320
+ async def chat_stream(request: ChatRequest):
321
+ """Streaming chat endpoint for continuous conversation"""
322
+ print(f"💬 Received chat with {len(request.messages)} messages, action: {request.action}")
323
+
324
+ # Use new format if available, otherwise fall back to legacy
325
+ current_chunk = request.currentChunk or request.chunk or "No specific chunk provided"
326
+ next_chunk = request.nextChunk or ""
327
+ action = request.action
328
+
329
+ document = request.document or """
330
+ # Auswertung Versuch F44: Zeeman Effekt
331
+ Dominic Holst, Moritz Pfau
332
+ October 23, 2020
333
+
334
+ ## 1 Magnetfeld und Hysterese
335
+ Zu Beginn des Versuchs haben wir mit Hilfe des Teslameters die Magnetfeldstärke B an der Position der Cd-Lampe bei verschiedenen Spulenströmen gemessen (siehe Messwerte in Tabelle 1 im Laborbuch). In Figure 1 sind die gemessenen Feldstärken als Funktion der Stromstärke aufgetragen.
336
+
337
+ Anhand der Fehlerbalken und der praktisch identischen Überlagerung der beiden linearen Fitgeraden für auf- und absteigende Stromstärken, wird deutlich, dass **keine Hystereseeffekte vorliegen**. Der lineare Fit wurde hierbei nur auf die Stromstärken bis einschl. 10A angewandt, da für größere Stromstärken das Magnetfeld nicht in direktem proportionalen Zusammenhang ansteigt. Dies ist mit Sättigungseffekten der Magnetisierung des Eisenkerns der verwendeten Spule zu erklären.
338
+
339
+ *Figure 1: Messung des Magnetfelds als Funktion der Stromstärke*
340
+
341
+ ## 2 Qualitative Beobachtung des Zeeman Effekts
342
+ Mit Hilfe der CMOS Kamera wurde das Spektrum des emittierten Lichts der Cadmiumlampe unter Verwendung des Lummer Gehercke Interferometers beobachtet. Die Beobachtungen wurden in longitudinaler und transversaler Richtung zum Magnetfeld durchgeführt.
343
+
344
+ ### 2.1 Longitudinale Richtung:
345
+ **ohne Filter:**
346
+ Es sind deutlich zwei Linien pro Ordnung zu erkennen. Dies sind die σ+ und σ' Linien. Die π Linie ist in longitudinaler Richtung nicht zu beobachten
347
+
348
+ **mit λ/4-Plättchen und Polarisationsfilter:**
349
+ Von der Cadmiumlampe aus betrachtet wird zuerst ein λ/4-Plättchen und danach ein Polarisationsfilter in den Strahlengang gebracht. Je nach Ausrichtung der Filter zueinander wird nun eine der beiden Linien ausgeblendet.
350
+
351
+ **-45° Winkel:**
352
+ Stehen λ/4-Plättchen und Polarisationsfilter zueinander im −45° Winkel, wird das zirkular polarisierte Licht der σ¯ Linie um 45° verschoben linear polarisiert und somit vom Polarisationsfilter abgeschirmt. Folglich ist in dieser Konstellation nur die linke der beiden σ Linien zu beobachten.
353
+
354
+ **+45° Winkel:**
355
+ Stehen λ/4-Plättchen und Polarisationsfilter zueinander im +45° Winkel, ist nach analogem Prinzip wie zuvor nur die rechte Linie auf dem Kamerabild zu beobachten.
356
+
357
+ *Figure 2: Bilder der CMOS Kamera in longitudinaler Richtung mit a) λ/4-Plättchen und Polarisationsfilter im −45° Winkel, b) ohne Filter und c) Filter im +45° Winkel*
358
+
359
+ ### 2.2 Transversale Richtung:
360
+ **ohne Filter:**
361
+ Es sind deutlich drei Linien pro Ordnung zu erkennen. Dies sind die σ⁺, π und σ⁻ Linien.
362
+
363
+ **mit Polarisationsfilter horizontal (in B-Feld Richtung):**
364
+ Die beiden σ-Linien sind vollständig ausgeblendet. Die π- Linie ist deutlich sichtbar.
365
+
366
+ **mit Polarisationsfilter vertikal (90° zu B-Feld Richtung):**
367
+ Die beiden σ-Linien sind klar sichtbar. Die π-Linie ist ausgeblendet.
368
+
369
+ *Figure 3: Bilder der CMOS Kamera in vertikaler Richtung mit a) keinem Filter, b) Polarisationsfilter horizontal und c) Polarisationsfilter vertikal*
370
+
371
+ Wie in Figure 3 gut zu erkennen ist, sind die ausgeblendeten Linien in beiden Konfigurationen weiterhin leicht sichtbar. Dies ist auf das nicht perfekt homogene Magnetfeld am Ort der Ca-Lampe zurückzuführen. Das Licht ist also nicht perfekt zirkular bzw. in B-Feld Richtung polarisiert, weshalb ein vollständiges Ausblenden im Experiment nicht zu beobachten ist.
372
+
373
+ ## 3 Spektroskopie des Zeemaneffekts
374
+
375
+ ### 3.1 Bestimmen des Zeemanshifts
376
+ Die Messdaten bei verschiedene Stromstärken wurden jeweils in einem Plot dargestellt. Um für den Fit möglichst saubere Messkurven des Spektrums zu verwenden, wurde die Messreihe bei I = 8A nicht in die Datenauswertung einbezogen, da die Aufspaltung der Cadmiumlinie nur schwer zu beobachten war. Das gleich gilt für die 8. Interferenzodnung, die nicht berücksichtigt wurde. Für die Datenauswertung fließen also die Nullte bis 7. Ordnung jeweils bei 9 bis 13 Ampere ein.
377
+ Als Funktion um die Messdaten zu fitten wurde ein Pseudo-Voigt-Profil verwendet. Die drei Kurven einer Ordnung wurden hierbei gemeinsam mit der Summe dreier Pseudo-Voigt-Profile gefittet. In Figure 4 sind exemplarisch anhand der Daten für I = 12A die Messdaten und der abschnittsweise Fit zu erkennen.
378
+
379
+ *Figure 4: Messdaten und Voigt-Fit bei Spulenstrom I = 12A*
380
+
381
+ Anhand der Fitparameter wird die Position der σ und π Linien bestimmt. Die Fehler der Fitparameter sind extrem klein (≈ 0,1px) und eigenen sich nicht als realistische Fehler für unsere weitere Rechnung. Als minimalen Fehler nehmen wir daher die Auflösung der Kamera an (1px) und skalieren alle Fehler so, dass der kleineste Fehler exakt 1px beträgt. Die anderen Fehler sind dann entsprechend linear skaliert größer. Dies berücksichtigt die unterschiedliche Qualität der Fits auf unterschiedliche Interferenz-Ordnungen, bringt die Fehler aber in einen experimentell realistischen Bereich.
382
+ Für die Berechnung des Zeemanshifts müssen die Verzerrungseffekte der Lummer-Gehrcke-Platte beachtet werden. Hierfür wird die Position der π-Linien gegen der Interferenzordnung k der entsprechenden Linie aufgetragen. Der funktionelle Zusammenhang dieser beiden Größen wird durch eine quadratische Funktion k = f(a) approximiert:
383
+
384
+ k = f(a) = ba² + ca + d (1)
385
+
386
+ Wir verwenden hier eine Taylor-Näherung für eine in der Realität deutlich kompliziertere Funktion. Dies ist aber, wie in Figure 5 gut ersichtlich, für unsere Zwecke weitaus ausreichend.
387
+ Die beiden σ-Linien können auf den quadratischen Fit f(a) projiziert werden, wodurch wir die jeweilige (nicht mehr ganzzahligen) Ordnung der σ-Linien erhalten. In Figure 5 ist (wieder exemplarisch für I = 12A) die optische Verzerrung der Platte aufgetragen.
388
+
389
+ *Figure 5: Verzerrungseffekte der Lummer-Gehrcke-Platte bei I = 12A*
390
+
391
+ Die Differenz zur ganzzahligen Ordnung der zugehörigen π-Linie ergibt δk. Für eine (kleine) Wellenlängenverschiebung δλ gilt:
392
+
393
+ δλ = δk / Δk * λ² / (2d * sqrt(n² − 1)) (2)
394
+
395
+ Für den Abstand Δk zweier Ordnungen gilt Δk = 1. Für die Wellenlänge λ der betrachten Linie verwenden wir den in Part 2 bestimmten Wert von λ = (643, 842 ± 0, 007)nm.
396
+ Wir kennen nun die Wellenlänge des Zeemanshift für jede von uns betrachtete Linie. Mit dem Zusammenhang zwischen Wellenlänge und Energie E = hc/λ lässt sich nun die Energieverschiebung der Linine bestimmen. Wir nehmen an, dass die Wellenlängenverschiebung δλ klein gegenüber der absoluten Wellenlänge λ ist, und erhalten daher für die Energieverschiebung δE in guter Näherung:
397
+
398
+ δE = (hc/λ²) * δλ (3)
399
+
400
+ Abschließend nehmen wir den Durchschnitt aller Werte δE für eine Stromstärke I.
401
+
402
+ ### 3.2 Bestimmen des Bohrschen Magnetons μB
403
+ Für die Energieverschiebung beim Zeemaneffekt gilt:
404
+
405
+ δE = μB · ml · B (4)
406
+
407
+ Da es sich bei der betrachteten Cadmiumlinie um einen ¹D₂ → ¹P₁ Übergang handelt gilt hier ml = ±1. Somit folgt für das Bohrsche Magneton μB als Funktion des Spulenstroms I:
408
+
409
+ μB(I) = δE(I) / B(I) (5)
410
+
411
+ Die Magnetfeldstärke B(I) wurde hier anhand der Messwerte aus Teil 1 des Experiments bestimmt.
412
+ Wir erhalten für jeden Spulenstrom I einen experimentell bestimmten Wert des Bohrschen Magnetons μB. Unsere Ergebnisse sind in Figure 6 graphisch dargestellt.
413
+
414
+ *Figure 6: Experimentell bestimmte Werte für das Bohrsche Magneton bei unterschiedlichen Spulenströmen I*
415
+
416
+ Für den experimentellen Mittelwert erhalten wir:
417
+ μB,exp = (10, 1 ± 0.8) · 10⁻²⁴ J/T
418
+
419
+ Der Literaturwert beträgt:
420
+ μB,lit = 9, 27400949 · 10⁻²⁴ J/T
421
+
422
+ Unsere experimentell ermittelte Wert weicht also um 1,2 Sigma vom Literaturwert ab. Die Abweichung ist folglich nicht signifikant.
423
+
424
+ ### 3.3 Kritische Betrachtung der Ergebnisse
425
+ Erfreulicherweise scheint unsere experimentelle Methode keine signifikante Abweichung zwischen Literaturwert und experimentellem Wert des Bohrschen Magnetons zu ergeben. Wir befinden uns mit unserem Wert im niedrigen 2-Sigma-Intervall. Dennoch ist kritisch anzumerken, dass wir einen vergleichsweise großen realtiven Fehler auf unser Messergebnis von 7,1% erhalten. Das bedeutet, unsere Abweichung ist zwar nicht sigifikant, dennoch weicht unser experimenteller Wert um knapp 10% vom Literaturwert ab. Der verwendete experimentelle Aufbau ist folglich nur bedingt für eine exakte Bestimmung des Bohrschen Magnetons geeigent.
426
+
427
+ Die beiden dominierenden Fehlerquellen sind zum einen die Bestimmung des Magnetfeldes B am Ort der Cadmium Lampe (Inhomogenitäten, exakte Platzierung der Lampe) und zum anderen die Wahl der Fehler der Positionen der π- und σ -Linien im Spektrum.
428
+ Zum Vergleich: Legt man den Fehler prinzipiell für alle Linien auf 1px, also die maximale Auflösung der Kamera, fest und verzichtet auf eine Skalierung der Fehler, beträgt die Abweichung des exp. Werts zum Literaturwert schon 2,8 Sigma. Wählt man analog für den Fehler der Linien 2px, da beispielsweise ein Maximum auch exakt zwischen zwei Pixelreihen liegen kann, liegt die Abweichung bei 1,4 Sigma.
429
+
430
+ ## 4 Quantitative Betrachtung des Spektrums
431
+
432
+ ### 4.1 Wellenlänge rote Cd-Linie
433
+
434
+ *Figure 7: Neonspektrum*
435
+
436
+ Zunächst wird der Untergrund von den Messdaten abgezogen, um Störungen durch Rauschen oder Sondereffekte wie kosmische Strahlung oder Umgebungsquellen zu eliminieren. Sollten sich in den Spektren negative Werte befinden, ist dies auf zufällige Unterschiede im Rauschen zurückzuführen. Anhand bekannter Linien des Neonspektrums werden den Pixeln nun Wellenlängen zugeordnet. Hierfür wurde der Bereich des Neonspektrums aufgenommen, in dem sich auch die rote Linie des Cadmiumspektrums befindet. In 7 sieht man das Neonspektrum und die Peaks, an die jeweils ein Voigt-Profil gelegt wurde. Jetzt kann man den identifizierten Linien ihre jeweilige Wellenlänge zuordnen und einen polynomiellen Zusammenhang finden. Wir haben uns für eine Gerade entschieden, die wie in Figure 8 zu sehen gut zu den Daten passt.
437
+ Schließlich wird ein Voigt-Profil an die gemessene rote Cd-Linie gelegt, wie in Figure 9 gezeigt. Umrechnung anhand der Kalibrierung führt auf einen Wert von λcd = (643,842 ± 0,007)nm. Dies befindet sich im 1σ-Bereich des Literaturwertes von λlit = 643, 84695nm. Der Fehler ist Ergebnis der Gauß'schen Fehlerfortpflanzung.
438
+
439
+ *Figure 8: Kalibrationsgerade*
440
+
441
+ ### 4.2 Kritische Betrachtung der Ergebnisse
442
+ Messwert und theoretische Vorhersage für die bestimmte Linie stimmen innerhalb statistischer Schwankungen überein. Dies ist umso interessanter, wenn man die Unsicherheit des Messergebnisses betrachtet, die kleiner als 0,002% ist. Der absolute Fehler ist, wenn man die Steigung der Kalibrationsgeraden betrachtet, kleiner als 1px. Er besteht ausschließlich aus Abweichungen der numerischen Fits. Berücksichtigt man Ungenauigkeiten des CMOS Sensors oder die Möglichkeit, dass je nach Lage des Messwerts auch eine Abweichung um weniger als 1px eine größere Messwertschwankung verursachen kann, da die Pixel nur diskrete Werte messen können, liegt eine nachträgliche Anpassung nahe. Skaliert man die Unsicherheit auf 1px, liegt der Fehler des Messwerts bei 0,012nm. Damit ist der relative Fehler weiterhin kleiner 0,005%.
443
+
444
+ Zur hohen Genauigkeit trägt vor allem das gute Messverfahren bei. Spektrometer und Datenaufnahme per Computer lassen wenig Raum für Abweichungen. Wie die Daten zeigen, haben wir dabei eine Quelle für einen möglichen großen systematischen Fehler umgangen: Die Kamera wurde auf das Spektrometer nur locker aufgesteckt. Hätte sich deren Position zwischen Neon- und Cadmiummmessung z.B. durch Erschütterung des Labortisches verändert, hätte die Energiekalibrierung nicht mehr zur Messung der Cadmiumlinie gepasst.
445
+
446
+ Abbildung 6 zeigt unerwartetes Verhalten. Obwohl der Magnet ausgeschaltet war, sind drei Maxima zu sehen, deren Flanken sehr steil abfallen. Vergleicht man mit den Messungen im Magnetfeld, ähneln sich die Strukturen. Möglich ist, dass die Eisenkernspule, in der sich die Lampe während der Messung befand eine Restmagnetisierung aufwies, die eine Aufspaltung herbeigeführt hat.
447
+
448
+ *Figure 9: Cadmium rote Linie*
449
+ """
450
+ # Create system prompt for research paper tutor with transition support
451
+ is_transition = action in ['skip', 'understood']
452
+
453
+ if is_transition:
454
+ system_prompt = f"""
455
+ You are PaperMentor, an expert academic tutor guiding the user through a continuous learning journey of an academic paper.
456
+
457
+ The user has just {action} the previous section and is transitioning to a new topic. This is part of a continuous conversation where you maintain context and adapt based on the user's actions.
458
+
459
+ User's Action: {action}
460
+
461
+ Previous Section:
462
+ {current_chunk}
463
+
464
+ New Section to Introduce:
465
+ {next_chunk}
466
+
467
+ Full Document for Context:
468
+ {document}
469
+
470
+ Your response should:
471
+ 1. **Acknowledge the transition**: Briefly reference their choice to {action} the previous section
472
+ 2. **Provide smooth continuity**: Connect the previous section to this new one naturally
473
+ 3. **Introduce the new section**: Present the new topic with enthusiasm and context
474
+ 4. **Adapt your approach**: If they skipped, perhaps adjust to be more engaging. If they understood, acknowledge their progress
475
+ 5. **Begin new exploration**: Start the 3-question sequence for this new section
476
+
477
+ Maintain the same conversational style and focus on phenomenological understanding.
478
+ """
479
+ else:
480
+ system_prompt = f"""
481
+ You are PaperMentor, an expert academic tutor. Your purpose is to guide a user to a deep, phenomenological understanding of an academic paper.
482
+
483
+ The user's primary goal is to: "phänomenologisch verstehen, was passiert, was beobachtet wurde und warum das so ist, mit wenig Fokus auf Formeln, sondern Fokus auf intuitivem Verständnis und dem experimentellen Ansatz." (phenomenologically understand what is happening, what was observed, and why, with little focus on formulas but a strong focus on intuitive understanding and the experimental approach).
484
+
485
+ Your entire interaction must be guided by this goal.
486
+
487
+ You will be given a specific chunk of the paper to discuss, as well as the full document for context.
488
+
489
+ ---
490
+ Current Chunk:
491
+ {current_chunk}
492
+ ---
493
+ Full Document for Context:
494
+ {document}
495
+ ---
496
+
497
+ Your interaction must follow this specific conversational flow:
498
+
499
+ 1. **Greeting and Contextualization:**
500
+ * Begin with a friendly greeting.
501
+ * First, briefly explain what this chunk is about in simple terms.
502
+ * Then, place this chunk within the larger context of the paper. Explain its purpose in the overall argument. For instance: "Here, the authors are presenting the core observation that the rest of the paper will attempt to explain," or "This section lays the theoretical groundwork for the experiment they describe later."
503
+
504
+ 2. **Socratic Questioning (The 3-Question Rule):**
505
+ * Your main task is to test and deepen the user's understanding through a series of exactly three questions about the current chunk.
506
+ * **First Question:** Ask a single, open-ended question that probes the user's intuitive grasp of the chunk's most important concept. The question must align with the user's goal (e.g., "In simple terms, what did the researchers actually observe here?" or "Why was it necessary for them to design the experiment in this specific way?"). **Always ask only one question at a time.**
507
+ * **If the user answers correctly:** Affirm their understanding (e.g., "Exactly," "That's a great way to put it") and immediately ask a *second, deeper question*. This question should build upon their correct answer, asking for more detail or to consider the implications.
508
+ * **If the user answers the second question correctly:** Again, affirm their response and ask a *third, even more probing question*. This final question should challenge them to think about the "why" or the broader significance of the information.
509
+ * **If the user answers incorrectly at any point:** Gently correct the misunderstanding. Provide a clear, intuitive explanation, always connecting it to the experimental observations and the "why." After your explanation, re-ask the question in a slightly different way to ensure they now understand, then continue the 3-question sequence.
510
+
511
+ 3. **Moving On:**
512
+ * After the user has successfully answered all three questions, congratulate them on their solid understanding.
513
+ * Conclude by explicitly giving them the choice to continue or stay. Say something like: "Excellent, it seems you have a very solid grasp of this part. Shall we move on to the next section, or is there anything here you'd like to explore further?"
514
+
515
+ **Important Behaviors:**
516
+ * **Language:** The entire conversation must be in English, as indicated by the user's goal.
517
+ * **Focus:** Always prioritize intuitive, conceptual, and experimental understanding over formal, mathematical details.
518
+ * **Pacing:** The flow is dictated by the user's successful answers. Move from one question to the next smoothly.
519
+ * **Structure:** Importantly, maintain a clear and logical flow in the conversation. Never loose track of the objective.
520
+ * **Markdown:** Output markdown if you think it is useful. Break your response into reasonable sections.
521
+ Begin the conversation.
522
+ """
523
+
524
+ anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY")
525
+ if not anthropic_api_key:
526
+ return {"role": "assistant", "content": "I'm sorry, but the chat service is not configured. Please check the API key configuration."}
527
+
528
+
529
+ client = anthropic.Anthropic(api_key=anthropic_api_key)
530
+
531
+ if not request.messages:
532
+ # No conversation yet — assistant should speak first
533
+ anthropic_messages = [
534
+ {"role": "user", "content": "Please start the conversation based on the provided context."}
535
+ ]
536
+ else:
537
+ anthropic_messages = [
538
+ {"role": msg.role, "content": msg.content}
539
+ for msg in request.messages
540
+ if msg.role in ["user", "assistant"]
541
+ ]
542
+ print(anthropic_messages)
543
+ # For transitions, add a dummy user message to trigger Claude response
544
+ if not any(msg["role"] == "user" for msg in anthropic_messages):
545
+ if is_transition:
546
+ anthropic_messages.append({"role": "user", "content": "Please continue to the next section."})
547
+ else:
548
+ def generate_error():
549
+ yield f"data: {json.dumps({'error': 'I did not receive your message. Could you please ask again?'})}\n\n"
550
+ return StreamingResponse(
551
+ media_type="text/event-stream",
552
+ content=generate_error(),
553
+ headers={"Cache-Control": "no-cache",
554
+ "Connection": "keep-alive",
555
+ "Access-Control-Allow-Origin": "*"},
556
+ )
557
+
558
+ def generate():
559
+ try:
560
+ with client.messages.stream(
561
+ model="claude-sonnet-4-20250514",
562
+ max_tokens=10000,
563
+ system=system_prompt, # system prompt here
564
+ messages=anthropic_messages,
565
+ ) as stream:
566
+ for text in stream.text_stream:
567
+ print(f"Raw text chunk: {repr(text)}")
568
+ yield f"data: {json.dumps(text)}\n\n"
569
+ yield f"data: {json.dumps({'done': True})}\n\n"
570
+ except Exception as e:
571
+ yield f"data: {json.dumps({'error': str(e)})}\n\n"
572
+
573
+ return StreamingResponse(
574
+ media_type="text/event_stream",
575
+ content=generate(),
576
+ headers={"Cache-Control": "no-cache",
577
+ "Connection": "keep-alive",
578
+ "Access-Control-Allow-Origin": "*"},
579
+ )
580
+
581
+
582
  # Mount static files for production deployment
583
  frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend")
584
  assets_path = os.path.join(frontend_path, "assets")
frontend/src/components/ChunkLoadingTips.jsx CHANGED
@@ -25,19 +25,21 @@ const ChunkLoadingTips = ({ message = "Preparing your document..." }) => {
25
  }, []);
26
 
27
  return (
28
- <div className="flex flex-col items-center justify-center h-full p-8 text-center">
29
- {/* Loading spinner */}
30
- <div className="relative mb-8">
31
- <div className="w-12 h-12 border-4 border-blue-200 rounded-full animate-spin border-t-blue-600"></div>
32
- </div>
 
33
 
34
- {/* Main message */}
35
- <h3 className="text-lg font-medium text-gray-900 mb-2">
36
- {message}
37
- </h3>
38
- <span key={currentTipIndex} className="inline-block animate-fade-in text-gray-500">
39
- {tips[currentTipIndex]}
40
- </span>
 
41
  </div>
42
  );
43
  };
 
25
  }, []);
26
 
27
  return (
28
+ <div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 z-50">
29
+ <div className="bg-white/90 backdrop-blur-sm rounded-xl shadow-lg border border-gray-200 p-6 max-w-sm text-center">
30
+ {/* Loading spinner */}
31
+ <div className="relative mb-4">
32
+ <div className="w-8 h-8 border-3 border-blue-200 rounded-full animate-spin border-t-blue-600 mx-auto"></div>
33
+ </div>
34
 
35
+ {/* Main message */}
36
+ <h3 className="text-sm font-medium text-gray-900 mb-3">
37
+ {message}
38
+ </h3>
39
+ <p key={currentTipIndex} className="text-xs text-gray-600 animate-fade-in leading-relaxed">
40
+ {tips[currentTipIndex]}
41
+ </p>
42
+ </div>
43
  </div>
44
  );
45
  };
frontend/src/components/ChunkPanel.jsx CHANGED
@@ -33,10 +33,143 @@ const ChunkPanel = ({
33
  // Only for initial chunk (0) and when not transitioning
34
  useEffect(() => {
35
  if (documentData && showChat && !hasChunkMessages(currentChunkIndex) && currentChunkIndex === 0 && !isTransitioning) {
36
- generateGreeting();
37
  }
38
  }, [currentChunkIndex, documentData, showChat, isTransitioning]);
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  const generateGreeting = async () => {
41
  setIsLoading(true);
42
  if (setWaitingForFirstResponse) {
@@ -79,6 +212,123 @@ const ChunkPanel = ({
79
  }
80
  };
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  const handleSend = async (text) => {
83
  const userMessage = { role: 'user', content: text, chunkIndex: currentChunkIndex };
84
  addMessageToChunk(userMessage, currentChunkIndex);
@@ -195,25 +445,17 @@ const ChunkPanel = ({
195
 
196
  </div>
197
 
198
- {/* Chat Interface - Only shown when showChat is true and not transitioning */}
199
- {showChat && !isLoading && !isTransitioning && (
 
200
  <SimpleChat
201
  messages={getGlobalChatHistory()}
202
  currentChunkIndex={currentChunkIndex}
203
  canEdit={canEditChunk(currentChunkIndex)}
204
- onSend={handleSend}
205
  isLoading={isLoading || isTransitioning}
206
  />
207
- )}
208
-
209
- {/* Loading Tips - Shown when generating greeting */}
210
- {showChat && isLoading && !hasChunkMessages(currentChunkIndex) && (
211
- <ChunkLoadingTips message="Preparing your lesson..." />
212
- )}
213
-
214
- {/* Transition Loading - Shown when moving between chunks */}
215
- {showChat && isTransitioning && (
216
- <ChunkLoadingTips message="Transitioning to next topic..." />
217
  )}
218
  </>
219
  );
 
33
  // Only for initial chunk (0) and when not transitioning
34
  useEffect(() => {
35
  if (documentData && showChat && !hasChunkMessages(currentChunkIndex) && currentChunkIndex === 0 && !isTransitioning) {
36
+ generateGreetingStreaming();
37
  }
38
  }, [currentChunkIndex, documentData, showChat, isTransitioning]);
39
 
40
+ const updateLastAssistantMessage = (delta) => {
41
+ const allMessages = getGlobalChatHistory();
42
+
43
+ const currentChunkMessages = allMessages.filter(msg => msg.chunkIndex === currentChunkIndex);
44
+ const lastAssistantInChunk = [...currentChunkMessages].reverse().find(msg => msg.role === 'assistant');
45
+
46
+ if (!lastAssistantInChunk) {
47
+ console.warn("No assistant message found for current chunk — adding new one.");
48
+ addMessageToChunk({ role: 'assistant', content: delta }, currentChunkIndex);
49
+ return;
50
+ }
51
+
52
+ const updatedMessages = allMessages.map(msg => {
53
+ if (msg === lastAssistantInChunk) {
54
+ return { ...msg, content: msg.content + (typeof delta === "string" ? delta : delta?.content || "") };
55
+ }
56
+ return msg;
57
+ });
58
+
59
+ updateGlobalChatHistory(updatedMessages);
60
+ };
61
+
62
+ const generateGreetingStreaming = async () => {
63
+ setIsLoading(true);
64
+ try {
65
+ const response = await fetch('/api/chat/stream', {
66
+ method: 'POST',
67
+ headers: { 'Content-Type': 'application/json' },
68
+ body: JSON.stringify({
69
+ messages: [],
70
+ currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '',
71
+ document: documentData ? JSON.stringify(documentData) : ''
72
+ })
73
+ });
74
+
75
+ const reader = response.body.getReader();
76
+ let shouldStop = false;
77
+
78
+ // Local snapshot to avoid stale reads
79
+ let localMessages = getGlobalChatHistory();
80
+ const createTempId = () => `assistant_${Date.now()}_${Math.random().toString(36).slice(2)}`;
81
+ let assistantId = null;
82
+
83
+ // SSE read buffer
84
+ let sseBuffer = '';
85
+
86
+ // Streaming smoothness buffer
87
+ let textBuffer = '';
88
+ let frameScheduled = false;
89
+
90
+ const flushBuffer = (isFinal = false) => {
91
+ if (!assistantId) return;
92
+
93
+ const lastMsg = localMessages[localMessages.length - 1];
94
+ if (lastMsg.id === assistantId) {
95
+ // Append buffered text
96
+ lastMsg.content += textBuffer;
97
+ textBuffer = '';
98
+ }
99
+ updateGlobalChatHistory([...localMessages]);
100
+ };
101
+
102
+ const scheduleFlush = () => {
103
+ if (!frameScheduled) {
104
+ frameScheduled = true;
105
+ requestAnimationFrame(() => {
106
+ flushBuffer();
107
+ frameScheduled = false;
108
+ });
109
+ }
110
+ };
111
+
112
+ while (!shouldStop) {
113
+ const { done, value } = await reader.read();
114
+ if (done) break;
115
+
116
+ sseBuffer += new TextDecoder().decode(value);
117
+ const parts = sseBuffer.split('\n\n');
118
+ sseBuffer = parts.pop(); // keep last partial
119
+
120
+ for (const part of parts) {
121
+ if (!part.startsWith('data:')) continue;
122
+ const jsonStr = part.slice(5).trim();
123
+ if (!jsonStr) continue;
124
+
125
+ let parsed;
126
+ try {
127
+ parsed = JSON.parse(jsonStr);
128
+ } catch (err) {
129
+ console.warn('Could not JSON.parse stream chunk', jsonStr);
130
+ continue;
131
+ }
132
+
133
+ if (parsed.error) {
134
+ console.error('streaming error', parsed.error);
135
+ shouldStop = true;
136
+ break;
137
+ }
138
+ if (parsed.done) {
139
+ shouldStop = true;
140
+ flushBuffer(true); // final flush, remove cursor
141
+ break;
142
+ }
143
+
144
+ const delta = typeof parsed === 'string' ? parsed : parsed?.content ?? '';
145
+
146
+ if (!assistantId) {
147
+ assistantId = createTempId();
148
+ localMessages.push({
149
+ id: assistantId,
150
+ role: 'assistant',
151
+ content: delta,
152
+ chunkIndex: currentChunkIndex
153
+ });
154
+ } else {
155
+ textBuffer += delta;
156
+ }
157
+
158
+ // Schedule smooth UI update
159
+ scheduleFlush();
160
+ }
161
+ }
162
+ } catch (error) {
163
+ console.error(error);
164
+ addMessageToChunk(
165
+ { role: 'assistant', content: 'Sorry, something went wrong. Please try again.' },
166
+ currentChunkIndex
167
+ );
168
+ } finally {
169
+ setIsLoading(false);
170
+ }
171
+ };
172
+
173
  const generateGreeting = async () => {
174
  setIsLoading(true);
175
  if (setWaitingForFirstResponse) {
 
212
  }
213
  };
214
 
215
+ const handleSendStreaming = async (text) => {
216
+ const userMessage = { role: 'user', content: text, chunkIndex: currentChunkIndex };
217
+ addMessageToChunk(userMessage, currentChunkIndex);
218
+ setIsLoading(true);
219
+
220
+ try {
221
+ // Get the updated messages after adding the user message
222
+ const updatedMessages = [...getGlobalChatHistory(), userMessage];
223
+
224
+ const response = await fetch('/api/chat/stream', {
225
+ method: 'POST',
226
+ headers: { 'Content-Type': 'application/json' },
227
+ body: JSON.stringify({
228
+ messages: updatedMessages,
229
+ currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '',
230
+ document: documentData ? JSON.stringify(documentData) : ''
231
+ })
232
+ });
233
+
234
+ const reader = await response.body.getReader();
235
+
236
+ let shouldStop = false;
237
+
238
+ // Local snapshot to avoid stale reads - include the user message we just added
239
+ let localMessages = updatedMessages;
240
+ const createTempId = () => `assistant_${Date.now()}_${Math.random().toString(36).slice(2)}`;
241
+ let assistantId = null;
242
+
243
+ // SSE read buffer
244
+ let sseBuffer = '';
245
+
246
+ // Streaming smoothness buffer
247
+ let textBuffer = '';
248
+ let frameScheduled = false;
249
+
250
+ const flushBuffer = (isFinal = false) => {
251
+ if (!assistantId) return;
252
+
253
+ const lastMsg = localMessages[localMessages.length - 1];
254
+ if (lastMsg.id === assistantId) {
255
+ // Append buffered text
256
+ lastMsg.content += textBuffer;
257
+ textBuffer = '';
258
+ }
259
+ updateGlobalChatHistory([...localMessages]);
260
+ };
261
+
262
+ const scheduleFlush = () => {
263
+ if (!frameScheduled) {
264
+ frameScheduled = true;
265
+ requestAnimationFrame(() => {
266
+ flushBuffer();
267
+ frameScheduled = false;
268
+ });
269
+ }
270
+ };
271
+ while (!shouldStop) {
272
+ const { done, value } = await reader.read();
273
+ if (done) break;
274
+
275
+ sseBuffer += new TextDecoder().decode(value);
276
+ const parts = sseBuffer.split('\n\n');
277
+ sseBuffer = parts.pop(); // keep last partial
278
+
279
+ for (const part of parts) {
280
+ if (!part.startsWith('data:')) continue;
281
+ const jsonStr = part.slice(5).trim();
282
+ if (!jsonStr) continue;
283
+
284
+ let parsed;
285
+ try {
286
+ parsed = JSON.parse(jsonStr);
287
+ } catch (err) {
288
+ console.warn('Could not JSON.parse stream chunk', jsonStr);
289
+ continue;
290
+ }
291
+
292
+ if (parsed.error) {
293
+ console.error('streaming error', parsed.error);
294
+ shouldStop = true;
295
+ break;
296
+ }
297
+ if (parsed.done) {
298
+ shouldStop = true;
299
+ flushBuffer(true); // final flush, remove cursor
300
+ break;
301
+ }
302
+
303
+ const delta = typeof parsed === 'string' ? parsed : parsed?.content ?? '';
304
+
305
+ if (!assistantId) {
306
+ assistantId = createTempId();
307
+ localMessages.push({
308
+ id: assistantId,
309
+ role: 'assistant',
310
+ content: delta,
311
+ chunkIndex: currentChunkIndex
312
+ });
313
+ } else {
314
+ textBuffer += delta;
315
+ }
316
+
317
+ // Schedule smooth UI update
318
+ scheduleFlush();
319
+ }
320
+ }
321
+ } catch (error) {
322
+ console.error(error);
323
+ addMessageToChunk(
324
+ { role: 'assistant', content: 'Sorry, something went wrong. Please try again.' },
325
+ currentChunkIndex
326
+ );
327
+ } finally {
328
+ setIsLoading(false);
329
+ }
330
+ };
331
+
332
  const handleSend = async (text) => {
333
  const userMessage = { role: 'user', content: text, chunkIndex: currentChunkIndex };
334
  addMessageToChunk(userMessage, currentChunkIndex);
 
445
 
446
  </div>
447
 
448
+ {/* Chat Interface - Always shown when showChat is true */}
449
+ {showChat && (
450
+ <div className="relative flex-1 overflow-hidden">
451
  <SimpleChat
452
  messages={getGlobalChatHistory()}
453
  currentChunkIndex={currentChunkIndex}
454
  canEdit={canEditChunk(currentChunkIndex)}
455
+ onSend={handleSendStreaming}
456
  isLoading={isLoading || isTransitioning}
457
  />
458
+ </div>
 
 
 
 
 
 
 
 
 
459
  )}
460
  </>
461
  );
frontend/src/components/SimpleChat.jsx CHANGED
@@ -1,15 +1,14 @@
1
- import { useState, useEffect, useRef } from 'react';
2
  import ReactMarkdown from 'react-markdown';
3
  import remarkMath from 'remark-math';
4
  import rehypeKatex from 'rehype-katex';
5
  import rehypeRaw from 'rehype-raw';
6
  import { getChatMarkdownComponents } from '../utils/markdownComponents.jsx';
7
 
8
-
9
  const SimpleChat = ({ messages, currentChunkIndex, canEdit, onSend, isLoading }) => {
10
  const [input, setInput] = useState('');
11
- const messagesEndRef = useRef(null);
12
- const currentChunkStartRef = useRef(null);
13
 
14
  const handleSubmit = (e) => {
15
  e.preventDefault();
@@ -18,28 +17,107 @@ const SimpleChat = ({ messages, currentChunkIndex, canEdit, onSend, isLoading })
18
  setInput('');
19
  };
20
 
21
- // Scroll to current chunk's first message when chunk changes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  useEffect(() => {
23
- if (currentChunkStartRef.current) {
24
- currentChunkStartRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
 
 
25
  }
26
- }, [currentChunkIndex]);
 
27
 
28
- // Find the first message of the current chunk
29
- const currentChunkFirstMessageIndex = messages.findIndex(msg => msg.chunkIndex === currentChunkIndex);
 
 
 
30
 
31
  return (
32
- <div className="flex flex-col h-full">
33
- {/* Messages */}
34
- <div className="flex-1 overflow-y-auto p-4 space-y-3">
 
 
35
  {messages.map((message, idx) => {
36
  const isCurrentChunk = message.chunkIndex === currentChunkIndex;
37
- const isFirstOfCurrentChunk = idx === currentChunkFirstMessageIndex;
38
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  return (
40
  <div
41
  key={idx}
42
- ref={isFirstOfCurrentChunk ? currentChunkStartRef : null}
43
  className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
44
  >
45
  <div
@@ -50,44 +128,44 @@ const SimpleChat = ({ messages, currentChunkIndex, canEdit, onSend, isLoading })
50
  }`}
51
  >
52
  <ReactMarkdown
53
- remarkPlugins={[remarkMath]}
54
- rehypePlugins={[rehypeRaw, rehypeKatex]}
55
- components={getChatMarkdownComponents()}
56
- >
57
- {message.content}
58
- </ReactMarkdown>
59
  </div>
60
  </div>
61
  );
62
  })}
63
 
64
- {isLoading && (
65
- <div className="flex justify-start">
66
- <div className="bg-gray-100 p-3 rounded-lg">
67
- <div className="flex space-x-1">
68
- <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"></div>
69
- <div
70
- className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"
71
- style={{ animationDelay: '0.1s' }}
72
- ></div>
73
- <div
74
- className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"
75
- style={{ animationDelay: '0.2s' }}
76
- ></div>
77
  </div>
78
- </div>
 
79
  </div>
80
  )}
81
  </div>
82
 
83
- {/* Input */}
84
  <form onSubmit={handleSubmit} className="p-4 border-t">
85
  <div className="flex space-x-2">
86
  <input
87
  type="text"
88
  value={input}
89
  onChange={(e) => setInput(e.target.value)}
90
- placeholder={canEdit ? "Type your message..." : "This chunk is completed - navigation only"}
91
  disabled={isLoading || !canEdit}
92
  className="flex-1 px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:text-gray-500"
93
  />
 
1
+ import { useState, useEffect, useRef, useMemo } from 'react';
2
  import ReactMarkdown from 'react-markdown';
3
  import remarkMath from 'remark-math';
4
  import rehypeKatex from 'rehype-katex';
5
  import rehypeRaw from 'rehype-raw';
6
  import { getChatMarkdownComponents } from '../utils/markdownComponents.jsx';
7
 
 
8
  const SimpleChat = ({ messages, currentChunkIndex, canEdit, onSend, isLoading }) => {
9
  const [input, setInput] = useState('');
10
+ const containerRef = useRef(null);
11
+ const anchorRef = useRef(null); // <- will be a tiny zero-height anchor BEFORE the bubble
12
 
13
  const handleSubmit = (e) => {
14
  e.preventDefault();
 
17
  setInput('');
18
  };
19
 
20
+ // Determine the latest message index for this chunk (same as you had)
21
+ const { anchorIndex, firstInChunkIndex } = useMemo(() => {
22
+ let first = -1;
23
+ let last = -1;
24
+ for (let i = 0; i < messages.length; i++) {
25
+ if (messages[i].chunkIndex === currentChunkIndex) {
26
+ if (first === -1) first = i;
27
+ last = i;
28
+ }
29
+ }
30
+ return { anchorIndex: last !== -1 ? last : first, firstInChunkIndex: first };
31
+ }, [messages, currentChunkIndex]);
32
+
33
+ // Scroll by scrolling the ZERO-HEIGHT anchor into view AFTER layout commits.
34
+ const scrollAfterLayout = () => {
35
+ requestAnimationFrame(() => {
36
+ requestAnimationFrame(() => {
37
+ if (anchorRef.current) {
38
+ // Scroll the anchor to the top of the nearest scrollable ancestor (your container).
39
+ anchorRef.current.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' });
40
+ } else if (containerRef.current) {
41
+ // fallback: go to top
42
+ containerRef.current.scrollTo({ top: 0, behavior: 'smooth' });
43
+ }
44
+ });
45
+ });
46
+ };
47
+
48
+ // When chunk changes, try to pin.
49
  useEffect(() => {
50
+ if (anchorIndex !== -1) {
51
+ scrollAfterLayout();
52
+ } else if (containerRef.current) {
53
+ requestAnimationFrame(() => containerRef.current.scrollTo({ top: 0, behavior: 'smooth' }));
54
  }
55
+ // eslint-disable-next-line react-hooks/exhaustive-deps
56
+ }, [currentChunkIndex, anchorIndex]);
57
 
58
+ // New messages: pin the new anchor after layout
59
+ useEffect(() => {
60
+ if (anchorIndex !== -1) scrollAfterLayout();
61
+ // eslint-disable-next-line react-hooks/exhaustive-deps
62
+ }, [messages.length, anchorIndex]);
63
 
64
  return (
65
+ <div className="flex flex-col h-full min-h-0">
66
+ <div
67
+ ref={containerRef}
68
+ className="flex-1 min-h-0 overflow-y-auto p-4 flex flex-col space-y-3"
69
+ >
70
  {messages.map((message, idx) => {
71
  const isCurrentChunk = message.chunkIndex === currentChunkIndex;
72
+ const isAnchor = idx === anchorIndex;
73
+
74
+ // Render a zero-height anchor just BEFORE the bubble for the anchor index.
75
+ if (isAnchor) {
76
+ return (
77
+ <div key={idx} className="flex flex-col">
78
+ {/* <-- ZERO-HEIGHT anchor: deterministic top-of-message alignment */}
79
+ <div ref={anchorRef} style={{ height: 0, margin: 0, padding: 0 }} />
80
+
81
+ <div className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}>
82
+ <div
83
+ className={`max-w-[90%] p-3 rounded-lg transition-opacity ${
84
+ message.role === 'user'
85
+ ? `bg-gray-100 text-white ${isCurrentChunk ? 'opacity-100' : 'opacity-40'}`
86
+ : `bg-white text-gray-900 ${isCurrentChunk ? 'opacity-100' : 'opacity-40'}`
87
+ }`}
88
+ >
89
+ <ReactMarkdown
90
+ remarkPlugins={[remarkMath]}
91
+ rehypePlugins={[rehypeRaw, rehypeKatex]}
92
+ components={getChatMarkdownComponents()}
93
+ >
94
+ {message.content}
95
+ </ReactMarkdown>
96
+ </div>
97
+ </div>
98
+
99
+ {isLoading && (
100
+ <div className="flex justify-start mt-3">
101
+ <div className="bg-gray-100 p-3 rounded-lg">
102
+ <div className="flex space-x-1">
103
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"></div>
104
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.1s' }}></div>
105
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.2s' }}></div>
106
+ </div>
107
+ </div>
108
+ </div>
109
+ )}
110
+
111
+ {/* filler to push remaining whitespace below the pinned message */}
112
+ <div className="flex-1" />
113
+ </div>
114
+ );
115
+ }
116
+
117
+ // Non-anchor message: render normally
118
  return (
119
  <div
120
  key={idx}
 
121
  className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
122
  >
123
  <div
 
128
  }`}
129
  >
130
  <ReactMarkdown
131
+ remarkPlugins={[remarkMath]}
132
+ rehypePlugins={[rehypeRaw, rehypeKatex]}
133
+ components={getChatMarkdownComponents()}
134
+ >
135
+ {message.content}
136
+ </ReactMarkdown>
137
  </div>
138
  </div>
139
  );
140
  })}
141
 
142
+ {/* if no messages in chunk yet, render typing+filler */}
143
+ {firstInChunkIndex === -1 && (
144
+ <div className="flex flex-col">
145
+ {isLoading && (
146
+ <div className="flex justify-start">
147
+ <div className="bg-gray-100 p-3 rounded-lg">
148
+ <div className="flex space-x-1">
149
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"></div>
150
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.1s' }}></div>
151
+ <div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.2s' }}></div>
152
+ </div>
153
+ </div>
 
154
  </div>
155
+ )}
156
+ <div className="flex-1" />
157
  </div>
158
  )}
159
  </div>
160
 
161
+ {/* Input (unchanged) */}
162
  <form onSubmit={handleSubmit} className="p-4 border-t">
163
  <div className="flex space-x-2">
164
  <input
165
  type="text"
166
  value={input}
167
  onChange={(e) => setInput(e.target.value)}
168
+ placeholder={canEdit ? 'Type your message...' : 'This chunk is completed - navigation only'}
169
  disabled={isLoading || !canEdit}
170
  className="flex-1 px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:text-gray-500"
171
  />