@@ -181,6 +181,7 @@ def __init__(self):
181181 self .skip_lines = 0
182182 self ._connected_apps = []
183183 self ._llm_provider = None
184+ self ._request_number = 0
184185
185186 def reset_history_position (self , _ : Buffer ) -> None :
186187 self .skip_lines = 0
@@ -346,7 +347,7 @@ async def error_catcher(buffer):
346347 try :
347348 await self ._trigger_llm_core (buffer )
348349 except Exception as e :
349- get_ipython ().log .error ("error" )
350+ get_ipython ().log .error ("error %s" , e )
350351 raise
351352
352353 # here we need a cancellable task so we can't just await the error caught
@@ -361,9 +362,8 @@ async def _trigger_llm_core(self, buffer: Buffer):
361362 provider to stream it's response back to us iteratively setting it as
362363 the suggestion on the current buffer.
363364
364- Unlike with JupyterAi, as we do not have multiple cell, the cell number
365- is always set to `0`, note that we _could_ set it to a new number each
366- time and ignore threply from past numbers.
365+ Unlike with JupyterAi, as we do not have multiple cell, the cell id
366+ is always set to `None`.
367367
368368 We set the prefix to the current cell content, but could also inset the
369369 rest of the history or even just the non-fail history.
@@ -385,10 +385,12 @@ async def _trigger_llm_core(self, buffer: Buffer):
385385
386386 hm = buffer .history .shell .history_manager
387387 prefix = self ._llm_prefixer (hm )
388- print ( prefix )
388+ get_ipython (). log . debug ( "prefix: %s" , prefix )
389389
390+ self ._request_number += 1
391+ request_number = self ._request_number
390392 request = jai_models .InlineCompletionRequest (
391- number = 0 ,
393+ number = request_number ,
392394 prefix = prefix + buffer .document .text ,
393395 suffix = "" ,
394396 mime = "text/x-python" ,
@@ -401,6 +403,9 @@ async def _trigger_llm_core(self, buffer: Buffer):
401403 async for reply_and_chunks in self ._llm_provider .stream_inline_completions (
402404 request
403405 ):
406+ if self ._request_number != request_number :
407+ # If a new suggestion was requested, skip processing this one.
408+ return
404409 if isinstance (reply_and_chunks , jai_models .InlineCompletionReply ):
405410 if len (reply_and_chunks .list .items ) > 1 :
406411 raise ValueError (
0 commit comments