@@ -4,10 +4,12 @@ import { getPromptCompletion } from "../prompt";
4
4
import { abortInterval , delay } from "./utils/intervals" ;
5
5
import { getAdditionalDocuments } from "./utils/getAdditionalDocuments" ;
6
6
import Logger from "../logger" ;
7
- import { sendCompletionRequest } from "./localCompletion" ;
7
+ import { sendCompletionRequestLocal } from "./localCompletion" ;
8
8
import { servers } from "../server" ;
9
9
import { configuration } from "../utils/configuration" ;
10
10
import { state } from "../utils/state" ;
11
+ import { sendCompletionsRequestCloud } from "./cloudCompletion" ;
12
+ import statusBar from "../statusBar" ;
11
13
12
14
const logCompletion = ( ) => {
13
15
const uuid = randomUUID ( ) ;
@@ -39,10 +41,12 @@ export const getInlineCompletionProvider = (
39
41
"inlineSuggestModeAuto"
40
42
) ;
41
43
44
+ // If the current mode is not auto and the trigger is automatic, don't suggest any completions.
42
45
if ( currentInlineSuggestModeAuto !== true && triggerAuto === true ) {
43
46
return [ ] ;
44
47
}
45
48
49
+ // If there is a selected completion item, don't suggest any completions.
46
50
if ( context . selectedCompletionInfo ) {
47
51
return [ ] ;
48
52
}
@@ -52,42 +56,14 @@ export const getInlineCompletionProvider = (
52
56
loggerCompletion . info ( "Completion: started" ) ;
53
57
54
58
const cancelled = await delay ( 250 , token ) ;
59
+
55
60
if ( cancelled ) {
56
61
loggerCompletion . info ( "Completion: canceled" ) ;
57
62
return [ ] ;
58
63
}
59
64
60
65
const { abortController, requestFinish } = abortInterval ( token ) ;
61
-
62
- const modelType = triggerAuto
63
- ? configuration . get ( "completion.autoMode" )
64
- : configuration . get ( "completion.manuallyMode" ) ;
65
-
66
- const serverUrl = servers [ modelType ] . serverUrl ;
67
-
68
- const additionalDocuments : vscode . TextDocument [ ] = configuration . get (
69
- "experimental.useopentabs"
70
- )
71
- ? await getAdditionalDocuments ( )
72
- : [ ] ;
73
-
74
- const prompt = await getPromptCompletion ( {
75
- activeDocument : document ,
76
- additionalDocuments : additionalDocuments ,
77
- position : position ,
78
- maxTokenExpect : triggerAuto ? maxToken : 2000 ,
79
- } ) ;
80
-
81
- const parameters = triggerAuto
82
- ? {
83
- n_predict : 128 ,
84
- stop : [ "\n" , "<|file_separator|>" ] ,
85
- }
86
- : {
87
- n_predict : 512 ,
88
- stop : [ "<|file_separator|>" ] ,
89
- temperature : 0.5 ,
90
- } ;
66
+ const { stopTask } = statusBar . startTask ( ) ;
91
67
92
68
try {
93
69
Logger . info (
@@ -97,49 +73,106 @@ export const getInlineCompletionProvider = (
97
73
sendTelemetry : true ,
98
74
}
99
75
) ;
76
+ if ( configuration . get ( "cloud.use" ) ) {
77
+ const prompt = await getPromptCompletion ( {
78
+ activeDocument : document ,
79
+ additionalDocuments : await getAdditionalDocuments ( ) ,
80
+ position : position ,
81
+ maxTokenExpect : 3300 ,
82
+ } ) ;
83
+ const completion = await sendCompletionsRequestCloud ( prompt , {
84
+ n_predict : 512 ,
85
+ temperature : 0.5 ,
86
+ controller : abortController ,
87
+ } ) ;
100
88
101
- const completion = await sendCompletionRequest (
102
- prompt ,
103
- parameters ,
104
- abortController ,
105
- loggerCompletion . uuid ( ) ,
106
- serverUrl
107
- ) ;
89
+ if ( completion === "" || completion === undefined ) {
90
+ return [ ] ;
91
+ }
108
92
109
- if ( completion === null ) {
110
- return [ ] ;
111
- }
93
+ if ( token . isCancellationRequested ) {
94
+ loggerCompletion . info (
95
+ "Request: canceled by new completion cancel token"
96
+ ) ;
112
97
113
- if ( token . isCancellationRequested ) {
114
- loggerCompletion . info (
115
- "Request: canceled by new completion cancel token"
116
- ) ;
98
+ return [ ] ;
99
+ }
117
100
118
- return [ ] ;
119
- }
120
- if ( triggerAuto ) {
121
- maxToken *= expectedTime / completion . timing ;
122
- }
101
+ return [
102
+ {
103
+ insertText : completion ,
104
+ range : new vscode . Range ( position , position ) ,
105
+ } ,
106
+ ] ;
107
+ } else {
108
+ const additionalDocuments : vscode . TextDocument [ ] = configuration . get (
109
+ "experimental.useopentabs"
110
+ )
111
+ ? await getAdditionalDocuments ( )
112
+ : [ ] ;
113
+
114
+ const prompt = await getPromptCompletion ( {
115
+ activeDocument : document ,
116
+ additionalDocuments : additionalDocuments ,
117
+ position : position ,
118
+ maxTokenExpect : triggerAuto ? maxToken : 2000 ,
119
+ } ) ;
120
+
121
+ const parameters = triggerAuto
122
+ ? {
123
+ n_predict : 128 ,
124
+ stop : [ "\n" , "<|file_separator|>" ] ,
125
+ }
126
+ : {
127
+ n_predict : 512 ,
128
+ stop : [ "<|file_separator|>" ] ,
129
+ temperature : 0.5 ,
130
+ } ;
131
+ const modelType = triggerAuto
132
+ ? configuration . get ( "completion.autoMode" )
133
+ : configuration . get ( "completion.manuallyMode" ) ;
134
+ const completion = await sendCompletionRequestLocal (
135
+ prompt ,
136
+ parameters ,
137
+ abortController ,
138
+ loggerCompletion . uuid ( ) ,
139
+ servers [ modelType ] . serverUrl
140
+ ) ;
123
141
124
- loggerCompletion . info ( `maxToken: ${ maxToken } ` ) ;
142
+ if ( completion === null ) {
143
+ return [ ] ;
144
+ }
125
145
126
- loggerCompletion . info ( "Request: finished" ) ;
146
+ if ( token . isCancellationRequested ) {
147
+ loggerCompletion . info (
148
+ "Request: canceled by new completion cancel token"
149
+ ) ;
127
150
128
- return [
129
- {
130
- insertText : completion . content ,
131
- range : new vscode . Range ( position , position ) ,
132
- } ,
133
- ] ;
151
+ return [ ] ;
152
+ }
153
+ if ( triggerAuto ) {
154
+ maxToken *= expectedTime / completion . timing ;
155
+ }
156
+ loggerCompletion . info ( "Request: finished" ) ;
157
+
158
+ return [
159
+ {
160
+ insertText : completion . content ,
161
+ range : new vscode . Range ( position , position ) ,
162
+ } ,
163
+ ] ;
164
+ }
134
165
} catch ( error ) {
135
166
const Error = error as Error ;
136
167
Logger . error ( error ) ;
137
168
138
169
const errorMessage = Error . message ;
139
170
vscode . window . showErrorMessage ( errorMessage ) ;
140
171
} finally {
172
+ stopTask ( ) ;
141
173
requestFinish ( ) ;
142
174
}
175
+ return [ ] ;
143
176
} ,
144
177
} ;
145
178
0 commit comments