@@ -104,6 +104,10 @@ local function machine_id()
104104 return hex
105105end
106106
107+ local function quick_hash (str )
108+ return # str .. str :sub (1 , 32 ) .. str :sub (- 32 )
109+ end
110+
107111local function find_config_path ()
108112 local config = vim .fn .expand (' $XDG_CONFIG_HOME' )
109113 if config and vim .fn .isdirectory (config ) > 0 then
346350
347351local Copilot = class (function (self , proxy , allow_insecure )
348352 self .history = {}
353+ self .embedding_cache = {}
349354 self .github_token = nil
350355 self .token = nil
351356 self .sessionid = nil
@@ -881,18 +886,34 @@ end
881886--- @param inputs table<CopilotChat.copilot.embed> : The inputs to embed
882887--- @param opts CopilotChat.copilot.embed.opts : Options for the request
883888function Copilot :embed (inputs , opts )
884- opts = opts or {}
885- local model = opts .model or ' text-embedding-3-small'
886- local chunk_size = opts .chunk_size or 15
887-
888889 if not inputs or # inputs == 0 then
889890 return {}
890891 end
891892
893+ -- Check which embeddings need to be fetched
894+ local cached_embeddings = {}
895+ local uncached_embeddings = {}
896+ for _ , embed in ipairs (inputs ) do
897+ if embed .content then
898+ local key = embed .filename .. quick_hash (embed .content )
899+ if self .embedding_cache [key ] then
900+ table.insert (cached_embeddings , self .embedding_cache [key ])
901+ else
902+ table.insert (uncached_embeddings , embed )
903+ end
904+ else
905+ table.insert (uncached_embeddings , embed )
906+ end
907+ end
908+
909+ opts = opts or {}
910+ local model = opts .model or ' text-embedding-3-small'
911+ local chunk_size = opts .chunk_size or 15
912+
892913 local out = {}
893914
894- for i = 1 , # inputs , chunk_size do
895- local chunk = vim .list_slice (inputs , i , i + chunk_size - 1 )
915+ for i = 1 , # uncached_embeddings , chunk_size do
916+ local chunk = vim .list_slice (uncached_embeddings , i , i + chunk_size - 1 )
896917 local body = vim .json .encode (generate_embedding_request (chunk , model ))
897918 local response , err = curl_post (
898919 ' https://api.githubcopilot.com/embeddings' ,
@@ -934,7 +955,16 @@ function Copilot:embed(inputs, opts)
934955 end
935956 end
936957
937- return out
958+ -- Cache embeddings
959+ for _ , embedding in ipairs (out ) do
960+ if embedding .content then
961+ local key = embedding .filename .. quick_hash (embedding .content )
962+ self .embedding_cache [key ] = embedding
963+ end
964+ end
965+
966+ -- Merge cached embeddings and newly fetched embeddings and return
967+ return vim .list_extend (out , cached_embeddings )
938968end
939969
940970--- Stop the running job
951981function Copilot :reset ()
952982 local stopped = self :stop ()
953983 self .history = {}
984+ self .embedding_cache = {}
954985 return stopped
955986end
956987
0 commit comments