1+ import createLRUCache from "../utils/lru-cache" ;
12
23// Remove all newlines, double spaces, etc
34function normalizeText ( src : string ) {
4- src = src . split ( '\n' ) . join ( ' ' ) ;
5- src = src . replace ( / \s + / gm, ' ' ) ;
6- return src ;
5+ src = src . split ( "\n" ) . join ( " " ) ;
6+ src = src . replace ( / \s + / gm, " " ) ;
7+ return src ;
78}
89
9- function extractPromptCacheKey ( args : { prefix : string , suffix : string | null } ) {
10- if ( args . suffix ) {
11- return normalizeText ( args . prefix + ' ##CURSOR## ' + args . suffix ) ;
12- } else {
13- return normalizeText ( args . prefix ) ;
14- }
10+ function extractPromptCacheKey ( args : {
11+ prefix : string ;
12+ suffix : string | null ;
13+ } ) {
14+ if ( args . suffix ) {
15+ return normalizeText ( args . prefix + " ##CURSOR## " + args . suffix ) ;
16+ } else {
17+ return normalizeText ( args . prefix ) ;
18+ }
1519}
1620
17- // TODO: make it LRU
18- let cache : { [ key : string ] : string | null } = { } ;
21+ const promptCache = createLRUCache < string , string | null > ( { maxSize : 1000 } ) ;
1922
20- export function getFromPromptCache ( args : { prefix : string , suffix : string | null } ) : string | undefined | null {
21- const key = extractPromptCacheKey ( args ) ;
22- return cache [ key ] ;
23+ export function getFromPromptCache ( args : {
24+ prefix : string ;
25+ suffix : string | null ;
26+ } ) : string | undefined | null {
27+ const key = extractPromptCacheKey ( args ) ;
28+ return promptCache . get ( key ) ;
2329}
2430
25- export function setPromptToCache ( args : { prefix : string , suffix : string | null , value : string | null } ) {
26- const key = extractPromptCacheKey ( args ) ;
27- cache [ key ] = args . value ;
28- }
31+ export function setPromptToCache ( args : {
32+ prefix : string ;
33+ suffix : string | null ;
34+ value : string | null ;
35+ } ) {
36+ const key = extractPromptCacheKey ( args ) ;
37+ promptCache . set ( key , args . value ) ;
38+ }
0 commit comments