-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from dcSpark/feature/add-chess-move-tool
feat: add chess-move tool
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { expect } from '@jest/globals'; | ||
import { getToolTestClient } from '../../src/test/utils'; | ||
import * as path from 'path'; | ||
|
||
describe('Chess Move Tool', () => { | ||
const toolPath = path.join(__dirname, 'tool.py'); | ||
const client = getToolTestClient(); | ||
|
||
it('makes a legal move from the start position', async () => { | ||
const startFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; | ||
const response = await client.executeToolFromFile(toolPath, { | ||
fen: startFen, | ||
move_uci: "e2e4" | ||
}); | ||
|
||
console.log(response); | ||
|
||
expect(response).toHaveProperty('new_fen'); | ||
expect(response).toHaveProperty('is_legal', true); | ||
// newFen should be different from startFen | ||
expect(typeof response.new_fen).toBe('string'); | ||
expect(response.new_fen).not.toBe(startFen); | ||
}); | ||
|
||
it('rejects an illegal move', async () => { | ||
const startFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; | ||
const response = await client.executeToolFromFile(toolPath, { | ||
fen: startFen, | ||
move_uci: "e2e5" // Pawn can't jump three squares | ||
}); | ||
expect(response.is_legal).toBe(false); | ||
// The fen should remain unchanged | ||
expect(response.new_fen).toBe(startFen); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"id": "chess-move", | ||
"name": "Chess Move", | ||
"version": "1.0.0", | ||
"description": "Apply a move in UCI format to a given FEN and return the resulting position", | ||
"author": "Example", | ||
"keywords": [ | ||
"chess", | ||
"move", | ||
"uci", | ||
"fen", | ||
"position", | ||
"game" | ||
], | ||
"configurations": { | ||
"type": "object", | ||
"properties": {}, | ||
"required": [] | ||
}, | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"fen": { | ||
"type": "string", | ||
"description": "FEN describing the current position" | ||
}, | ||
"move_uci": { | ||
"type": "string", | ||
"description": "Move in UCI format (e.g. 'e2e4')" | ||
} | ||
}, | ||
"required": ["fen", "move_uci"] | ||
}, | ||
"result": { | ||
"type": "object", | ||
"properties": { | ||
"new_fen": { | ||
"type": "string" | ||
}, | ||
"is_legal": { | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": ["new_fen", "is_legal"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"categoryId": "f4906ba5-16bb-445d-9241-85422cf5a055" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# /// script | ||
# dependencies = [ | ||
# "python-chess>=1.999" | ||
# ] | ||
# /// | ||
import chess | ||
from typing import Dict, Any, Optional, List | ||
|
||
class CONFIG: | ||
pass | ||
|
||
class INPUTS: | ||
fen: str | ||
move_uci: str | ||
|
||
class OUTPUT: | ||
new_fen: str | ||
is_legal: bool | ||
|
||
async def run(c: CONFIG, p: INPUTS) -> OUTPUT: | ||
if not p.fen: | ||
raise ValueError("No FEN provided") | ||
if not p.move_uci: | ||
raise ValueError("No UCI move provided") | ||
|
||
board = chess.Board() | ||
try: | ||
board.set_fen(p.fen) | ||
except ValueError: | ||
raise ValueError("Invalid FEN") | ||
|
||
# Validate the move format, e.g. "e2e4", "e7e8q" | ||
if len(p.move_uci) < 4 or len(p.move_uci) > 5: | ||
raise ValueError(f"Move '{p.move_uci}' not in typical UCI format") | ||
|
||
move = None | ||
try: | ||
move = board.parse_uci(p.move_uci) | ||
except: | ||
pass | ||
|
||
result = OUTPUT() | ||
if move and move in board.legal_moves: | ||
board.push(move) | ||
result.is_legal = True | ||
result.new_fen = board.fen() | ||
else: | ||
result.is_legal = False | ||
result.new_fen = board.fen() # unchanged | ||
|
||
return result |