@@ -3,6 +3,49 @@ export function extractCommandPayload(rawText = "", commandName) {
33 return String ( rawText ) . replace ( pattern , "" ) . trim ( ) ;
44}
55
6+ function levenshteinDistance ( a , b ) {
7+ const left = String ( a ) ;
8+ const right = String ( b ) ;
9+ const dp = Array . from ( { length : left . length + 1 } , ( ) => new Array ( right . length + 1 ) . fill ( 0 ) ) ;
10+
11+ for ( let i = 0 ; i <= left . length ; i += 1 ) dp [ i ] [ 0 ] = i ;
12+ for ( let j = 0 ; j <= right . length ; j += 1 ) dp [ 0 ] [ j ] = j ;
13+
14+ for ( let i = 1 ; i <= left . length ; i += 1 ) {
15+ for ( let j = 1 ; j <= right . length ; j += 1 ) {
16+ const cost = left [ i - 1 ] === right [ j - 1 ] ? 0 : 1 ;
17+ dp [ i ] [ j ] = Math . min (
18+ dp [ i - 1 ] [ j ] + 1 ,
19+ dp [ i ] [ j - 1 ] + 1 ,
20+ dp [ i - 1 ] [ j - 1 ] + cost
21+ ) ;
22+ }
23+ }
24+
25+ return dp [ left . length ] [ right . length ] ;
26+ }
27+
28+ export function suggestClosestWord ( input , candidates , maxDistance = 2 ) {
29+ const normalizedInput = String ( input || "" ) . trim ( ) . toLowerCase ( ) ;
30+ if ( ! normalizedInput ) return "" ;
31+
32+ let best = "" ;
33+ let bestDistance = Number . POSITIVE_INFINITY ;
34+
35+ for ( const candidate of candidates ) {
36+ const normalizedCandidate = String ( candidate || "" ) . trim ( ) . toLowerCase ( ) ;
37+ if ( ! normalizedCandidate ) continue ;
38+
39+ const distance = levenshteinDistance ( normalizedInput , normalizedCandidate ) ;
40+ if ( distance < bestDistance ) {
41+ best = normalizedCandidate ;
42+ bestDistance = distance ;
43+ }
44+ }
45+
46+ return bestDistance <= maxDistance ? best : "" ;
47+ }
48+
649export function buildPlanPrompt ( task ) {
750 return [
851 "Planning mode only." ,
0 commit comments