trigo / trigo-web /backend /dist /inc /trigoTreeAgent.d.ts
k-l-lambda's picture
feat: room rename and room switching with confirmation
6f4808d
/**
* Trigo Tree Agent - AI agent using tree attention for efficient move evaluation
*
* Uses evaluation mode ONNX model to score all valid moves in parallel.
* Organizes moves as a prefix tree where branches with same head token are merged.
*/
import { ModelInferencer } from "./modelInferencer";
import { TrigoGame } from "./trigo/game";
import type { Move } from "./trigo/types";
export interface ScoredMove {
move: Move;
score: number;
notation: string;
}
export declare class TrigoTreeAgent {
private inferencer;
private readonly START_TOKEN;
constructor(inferencer: ModelInferencer);
/**
* Convert Stone type to player string
*/
private stoneToPlayer;
/**
* Encode a position to TGN notation (3 characters for 5×5×5 board)
*/
private positionToTGN;
/**
* Convert string to byte tokens (ASCII encoding)
*/
private stringToTokens;
/**
* Build prefix tree from token arrays using recursive merging
* Merges branches with the same token at EVERY level
*
* Algorithm:
* 1. Group sequences by their first token
* 2. For each group:
* - Create one node for the shared first token
* - Extract remaining tokens (residues)
* - Recursively build subtree from residues
* 3. Combine all subtrees and build attention mask
*
* Example for ["aa", "ab", "ba", "bb"]:
* Level 1: Group by first token → 'a': ["a","b"], 'b': ["a","b"]
* Level 2: Within 'a' group, build subtree for ["a","b"]
* Within 'b' group, build subtree for ["a","b"]
* Result: Two branches, each with properly merged second-level nodes
*
* @param tokenArrays - Array of token arrays
* @returns Flattened token array (length m), mask matrix (m×m), and move-to-position mapping
*/
private buildPrefixTree;
/**
* Build tree structure for all valid moves
* Returns prefix tokens and tree structure for batch evaluation
*/
private buildMoveTree;
/**
* Get tree structure for visualization (public method)
*/
getTreeStructure(game: TrigoGame, moves: Move[]): {
evaluatedIds: number[];
mask: number[];
parent: Array<number | null>;
moveData: Array<{
move: Move;
notation: string;
leafPos: number;
}>;
};
/**
* Select move using tree attention with optional temperature sampling
* @param game Current game state
* @param temperature Sampling temperature (0 = greedy, higher = more random)
* @returns Selected move (position or Pass if no valid positions)
*/
selectMove(game: TrigoGame, temperature?: number): Promise<Move>;
/**
* Select best move using tree attention (greedy, temperature=0)
* Evaluates all valid moves in a single inference call
* Pass is excluded from model prediction - returned directly if no positions available
*/
selectBestMove(game: TrigoGame): Promise<Move>;
/**
* Sample a move from scored moves using temperature
*/
private sampleMove;
/**
* Score all moves using tree attention (batch evaluation)
*/
scoreMoves(game: TrigoGame, moves: Move[]): Promise<ScoredMove[]>;
}
//# sourceMappingURL=trigoTreeAgent.d.ts.map