Skip to content

Commit 364a54d

Browse files
Fix depth configuration bug and add live node updates during analysis
Co-authored-by: kevinjosethomas <46242684+kevinjosethomas@users.noreply.github.com>
1 parent 67beb2a commit 364a54d

5 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const useAnalysisController = (
5353
game.tree,
5454
currentMaiaModel,
5555
setAnalysisState,
56+
controller.setCurrentNode,
5657
)
5758

5859
const availableMoves = useMemo(() => {

src/hooks/useAnalysisController/useGameAnalysis.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const useGameAnalysis = (
2222
gameTree: GameTree | null,
2323
currentMaiaModel: string,
2424
setAnalysisState: React.Dispatch<React.SetStateAction<number>>,
25+
setCurrentNode?: (node: GameNode) => void,
2526
) => {
2627
const maia = useContext(MaiaEngineContext)
2728
const stockfish = useContext(StockfishEngineContext)
@@ -124,17 +125,27 @@ export const useGameAnalysis = (
124125
}
125126

126127
// Analyze with Stockfish if not already at target depth
128+
const shouldAnalyze =
129+
!node.analysis.stockfish ||
130+
node.analysis.stockfish.depth < config.targetDepth
131+
console.log(
132+
`Node analysis check - hasStockfish: ${!!node.analysis.stockfish}, currentDepth: ${node.analysis.stockfish?.depth || 'none'}, targetDepth: ${config.targetDepth}, shouldAnalyze: ${shouldAnalyze}`,
133+
)
134+
127135
if (
128136
!analysisController.current.cancelled &&
129137
stockfish.isReady() &&
130-
(!node.analysis.stockfish ||
131-
node.analysis.stockfish.depth < config.targetDepth)
138+
shouldAnalyze
132139
) {
133140
try {
134141
const chess = new Chess(node.fen)
142+
console.log(
143+
`Starting Stockfish analysis for node with target depth: ${config.targetDepth}`,
144+
)
135145
const evaluationStream = stockfish.streamEvaluations(
136146
chess.fen(),
137147
chess.moves().length,
148+
config.targetDepth,
138149
)
139150

140151
if (evaluationStream) {
@@ -149,8 +160,15 @@ export const useGameAnalysis = (
149160
node.addStockfishAnalysis(evaluation, currentMaiaModel)
150161
setAnalysisState((state) => state + 1)
151162

163+
console.log(
164+
`Received evaluation at depth ${evaluation.depth}, target: ${config.targetDepth}`,
165+
)
166+
152167
// Stop when we reach target depth
153168
if (evaluation.depth >= config.targetDepth) {
169+
console.log(
170+
`Reached target depth ${config.targetDepth}, stopping analysis`,
171+
)
154172
break
155173
}
156174
}
@@ -218,6 +236,11 @@ export const useGameAnalysis = (
218236
const node = mainLine[i]
219237
analysisController.current.currentNode = node
220238

239+
// Update the UI to show the current node being analyzed (live update)
240+
if (setCurrentNode) {
241+
setCurrentNode(node)
242+
}
243+
221244
const moveDisplay = node.san || node.move || `Position ${i + 1}`
222245

223246
setProgress((prev) => ({
@@ -238,7 +261,14 @@ export const useGameAnalysis = (
238261
}))
239262

240263
analysisController.current.currentNode = null
241-
}, [gameTree, progress.isAnalyzing, stockfish, maia.status, analyzeNode])
264+
}, [
265+
gameTree,
266+
progress.isAnalyzing,
267+
stockfish,
268+
maia.status,
269+
analyzeNode,
270+
setCurrentNode,
271+
])
242272

243273
const cancelAnalysis = useCallback(() => {
244274
analysisController.current.cancelled = true

src/providers/StockfishEngineContextProvider/StockfishEngineContextProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export const StockfishEngineContextProvider: React.FC<{
2424
}
2525

2626
const streamEvaluations = useCallback(
27-
(fen: string, legalMoveCount: number) => {
27+
(fen: string, legalMoveCount: number, depth?: number) => {
2828
if (!engineRef.current) {
2929
console.error('Engine not initialized')
3030
return null
3131
}
32-
return engineRef.current.streamEvaluations(fen, legalMoveCount)
32+
return engineRef.current.streamEvaluations(fen, legalMoveCount, depth)
3333
},
3434
[],
3535
)

src/providers/StockfishEngineContextProvider/engine.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Engine {
6060
async *streamEvaluations(
6161
fen: string,
6262
legalMoveCount: number,
63+
targetDepth = 18,
6364
): AsyncGenerator<StockfishEvaluation> {
6465
if (this.stockfish && this.isReady) {
6566
if (typeof global.gc === 'function') {
@@ -81,7 +82,7 @@ class Engine {
8182

8283
this.sendMessage('ucinewgame')
8384
this.sendMessage(`position fen ${fen}`)
84-
this.sendMessage('go depth 18')
85+
this.sendMessage(`go depth ${targetDepth}`)
8586

8687
while (this.isEvaluating) {
8788
try {

src/types/analysis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface StockfishEngine {
1717
streamEvaluations: (
1818
fen: string,
1919
moveCount: number,
20+
depth?: number,
2021
) => AsyncIterable<StockfishEvaluation> | null
2122
}
2223

0 commit comments

Comments
 (0)