From 6c215cb3c4b5c1ed23f4be93c3b5797bda974c00 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 27 Nov 2025 20:37:43 +0100 Subject: [PATCH] fix: Correctly generate FEN positions before/after moves in move exchange The previous implementation tried to use undo() on a Chess object created from a FEN string, which has no move history. This resulted in all three FEN positions being identical. Now we use the game object's PGN (which contains full move history) and load it into temporary Chess objects before undoing moves. This correctly generates: - FEN before user's move (undo both computer and user moves) - FEN after user's move (undo only computer move) - FEN after computer's reply (current position) This ensures the LLM receives accurate position context for each stage of the move exchange. --- src/components/Tutor.tsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/Tutor.tsx b/src/components/Tutor.tsx index 033dfbf..c32a0db 100644 --- a/src/components/Tutor.tsx +++ b/src/components/Tutor.tsx @@ -239,11 +239,22 @@ IMPORTANT CONTEXT: } // Get FEN before user's move (need to undo both moves) - const tempGame = new Chess(currentFen); - tempGame.undo(); // Undo computer move - const fenAfterUserMove = tempGame.fen(); - tempGame.undo(); // Undo user move - const fenBeforeUserMove = tempGame.fen(); + // We need to use the game object which has the full move history + const history = game.history({ verbose: true }); + + // Current position is after both user and computer moves + // To get FEN after user move, we need to undo the computer move + const tempGame1 = new Chess(); + tempGame1.loadPgn(game.pgn()); + tempGame1.undo(); // Undo computer move + const fenAfterUserMove = tempGame1.fen(); + + // To get FEN before user move, we need to undo both moves + const tempGame2 = new Chess(); + tempGame2.loadPgn(game.pgn()); + tempGame2.undo(); // Undo computer move + tempGame2.undo(); // Undo user move + const fenBeforeUserMove = tempGame2.fen(); const prompt = ` [SYSTEM TRIGGER: move_exchange]