11import vscode from 'vscode' ;
2- import { info } from '../modules/log' ;
2+ import { info , warn } from '../modules/log' ;
33import { autocomplete } from './autocomplete' ;
44import { preparePrompt } from './preparePrompt' ;
55import { AsyncLock } from '../modules/lock' ;
@@ -19,116 +19,121 @@ export class PromptProvider implements vscode.InlineCompletionItemProvider {
1919
2020async provideInlineCompletionItems ( document :vscode . TextDocument , position :vscode . Position , context :vscode . InlineCompletionContext , token :vscode . CancellationToken ) :Promise < vscode . InlineCompletionItem [ ] | vscode . InlineCompletionList | undefined | null > {
2121
22- // Ignore unsupported documents
23- if ( ! isSupported ( document ) ) {
24- info ( `Unsupported document:${ document . uri . toString ( ) } ignored.` ) ;
25- return ;
26- }
27-
28- // Ignore if not needed
29- if ( isNotNeeded ( document , position ) ) {
30- info ( 'No inline completion required' ) ;
31- return ;
32- }
22+ try {
3323
34- // Ignoreif already canceled
35- if ( token . isCancellationRequested ) {
36- info ( `Canceled before AI completion .` ) ;
37- return ;
38- }
24+ // Ignoreunsupported documents
25+ if ( ! isSupported ( document ) ) {
26+ info ( `Unsupported document: ${ document . uri . toString ( ) } ignored .` ) ;
27+ return ;
28+ }
3929
40- // Execute in lock
41- return await this . lock . inLock ( async ( ) => {
30+ // Ignore if not needed
31+ if ( isNotNeeded ( document , position ) ) {
32+ info ( 'No inline completion required' ) ;
33+ return ;
34+ }
4235
43- // Prepare context
44- let prepared = await preparePrompt ( document , position , context ) ;
36+ // Ignore if already canceled
4537if ( token . isCancellationRequested ) {
4638info ( `Canceled before AI completion.` ) ;
4739return ;
4840}
4941
50- //Result
51- let res : string | null = null ;
42+ //Execute in lock
43+ return await this . lock . inLock ( async ( ) => {
5244
53- // Check if in cache
54- let cached = getFromPromptCache ( {
55- prefix :prepared . prefix ,
56- suffix :prepared . suffix
57- } ) ;
45+ // Prepare context
46+ let prepared = await preparePrompt ( document , position , context ) ;
47+ if ( token . isCancellationRequested ) {
48+ info ( `Canceled before AI completion.` ) ;
49+ return ;
50+ }
5851
59- // If not cached
60- if ( cached === undefined ) {
52+ // Result
53+ let res : string | null = null ;
6154
62- // Config
63- let config = vscode . workspace . getConfiguration ( 'inference' ) ;
64- let endpoint = config . get ( 'endpoint' ) as string ;
65- let model = config . get ( 'model' ) as string ;
66- if ( endpoint . endsWith ( '/' ) ) {
67- endpoint = endpoint . slice ( 0 , endpoint . length - 1 ) ;
68- }
55+ // Check if in cache
56+ let cached = getFromPromptCache ( {
57+ prefix :prepared . prefix ,
58+ suffix :prepared . suffix
59+ } ) ;
6960
70- // Update status
71- this . statusbar . text = `$(sync~spin) Llama Coder` ;
72- try {
61+ // If not cached
62+ if ( cached === undefined ) {
7363
74- // Check model exists
75- let modelExists = await ollamaCheckModel ( endpoint , model ) ;
76- if ( token . isCancellationRequested ) {
77- info ( `Canceled after AI completion.` ) ;
78- return ;
64+ // Config
65+ let config = vscode . workspace . getConfiguration ( 'inference' ) ;
66+ let endpoint = config . get ( 'endpoint' ) as string ;
67+ let model = config . get ( 'model' ) as string ;
68+ if ( endpoint . endsWith ( '/' ) ) {
69+ endpoint = endpoint . slice ( 0 , endpoint . length - 1 ) ;
7970}
8071
81- // Download model if not exists
82- if ( ! modelExists ) {
83- this . statusbar . text = `$(sync~spin) Downloading` ;
84- await ollamaDownloadModel ( endpoint , model ) ;
85- this . statusbar . text = `$(sync~spin) Llama Coder` ;
72+ // Update status
73+ this . statusbar . text = `$(sync~spin) Llama Coder` ;
74+ try {
75+
76+ // Check model exists
77+ let modelExists = await ollamaCheckModel ( endpoint , model ) ;
78+ if ( token . isCancellationRequested ) {
79+ info ( `Canceled after AI completion.` ) ;
80+ return ;
81+ }
82+
83+ // Download model if not exists
84+ if ( ! modelExists ) {
85+ this . statusbar . text = `$(sync~spin) Downloading` ;
86+ await ollamaDownloadModel ( endpoint , model ) ;
87+ this . statusbar . text = `$(sync~spin) Llama Coder` ;
88+ }
89+ if ( token . isCancellationRequested ) {
90+ info ( `Canceled after AI completion.` ) ;
91+ return ;
92+ }
93+
94+ // Run AI completion
95+ info ( `Running AI completion...` ) ;
96+ res = await autocomplete ( {
97+ prefix :prepared . prefix ,
98+ suffix :prepared . suffix ,
99+ endpoint :endpoint ,
100+ model :model ,
101+ canceled :( ) => token . isCancellationRequested ,
102+ } ) ;
103+ info ( `AI completion completed:${ res } ` ) ;
104+
105+ // Put to cache
106+ setPromptToCache ( {
107+ prefix :prepared . prefix ,
108+ suffix :prepared . suffix ,
109+ value :res
110+ } ) ;
111+ } finally {
112+ this . statusbar . text = `$(chip) Llama Coder` ;
86113}
87- if ( token . isCancellationRequested ) {
88- info ( `Canceled after AI completion.` ) ;
89- return ;
114+ } else {
115+ if ( cached !== null ) {
116+ res = cached ;
90117}
91-
92- // Run AI completion
93- info ( `Running AI completion...` ) ;
94- res = await autocomplete ( {
95- prefix :prepared . prefix ,
96- suffix :prepared . suffix ,
97- endpoint :endpoint ,
98- model :model ,
99- canceled :( ) => token . isCancellationRequested ,
100- } ) ;
101- info ( `AI completion completed:${ res } ` ) ;
102-
103- // Put to cache
104- setPromptToCache ( {
105- prefix :prepared . prefix ,
106- suffix :prepared . suffix ,
107- value :res
108- } ) ;
109- } finally {
110- this . statusbar . text = `$(chip) Llama Coder` ;
111118}
112- } else {
113- if ( cached !== null ) {
114- res = cached ;
119+ if ( token . isCancellationRequested ) {
120+ info ( `Canceled after AI completion.` ) ;
121+ return ;
115122}
116- }
117- if ( token . isCancellationRequested ) {
118- info ( `Canceled after AI completion.` ) ;
119- return ;
120- }
121123
122- // Return result
123- if ( res && res . trim ( ) !== '' ) {
124- return [ {
125- insertText :res ,
126- range :new vscode . Range ( position , position ) ,
127- } ] ;
128- }
124+ // Return result
125+ if ( res && res . trim ( ) !== '' ) {
126+ return [ {
127+ insertText :res ,
128+ range :new vscode . Range ( position , position ) ,
129+ } ] ;
130+ }
129131
130- // Nothing to complete
131- return ;
132- } ) ;
132+ // Nothing to complete
133+ return ;
134+ } ) ;
135+ } catch ( e ) {
136+ warn ( 'Error during inference:' , e ) ;
137+ }
133138}
134139}