1+ /* eslint-disable no-control-regex */
12import { EditorUIProps , monacoTypes } from '@remix-ui/editor' ;
3+ import axios , { AxiosResponse } from 'axios'
24const controller = new AbortController ( ) ;
35const { signal } = controller ;
46const result : string = ''
@@ -8,7 +10,7 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
810 monaco : any
911 constructor ( props : any , monaco : any ) {
1012 this . props = props
11- this . monaco = monaco
13+ this . monaco = monaco
1214 }
1315
1416 async provideInlineCompletions ( model : monacoTypes . editor . ITextModel , position : monacoTypes . Position , context : monacoTypes . languages . InlineCompletionContext , token : monacoTypes . CancellationToken ) : Promise < monacoTypes . languages . InlineCompletions < monacoTypes . languages . InlineCompletion > > {
@@ -29,6 +31,34 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
2931 return ;
3032 }
3133
34+ try {
35+ const isActivate = await this . props . plugin . call ( 'copilot-suggestion' , 'isActivate' )
36+ if ( ! isActivate ) return
37+ } catch ( err ) {
38+ return ;
39+ }
40+
41+ try {
42+ const split = word . split ( '\n' )
43+ if ( ! split . length ) return
44+ const ask = split [ split . length - 2 ] . trimStart ( )
45+ if ( split [ split . length - 1 ] . trim ( ) === '' && ask . startsWith ( '///' ) ) {
46+ // use the code generation model
47+ const { data} = await axios . post ( 'https://gpt-chat.remixproject.org/infer' , { comment : ask . replace ( '///' , '' ) } )
48+ const parsedData = JSON . parse ( data ) . trimStart ( )
49+ console . log ( 'parsedData' , parsedData )
50+ const item : monacoTypes . languages . InlineCompletion = {
51+ insertText : parsedData
52+ } ;
53+ return {
54+ items : [ item ] ,
55+ enableForwardStability : true
56+ }
57+ }
58+ } catch ( e ) {
59+ console . error ( e )
60+ }
61+
3262 // abort if there is a signal
3363 if ( token . isCancellationRequested ) {
3464 console . log ( 'aborted' )
@@ -38,18 +68,17 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
3868 let result
3969 try {
4070 result = await this . props . plugin . call ( 'copilot-suggestion' , 'suggest' , word )
41- } catch ( err ) {
71+ } catch ( err ) {
4272 return
4373 }
44-
74+
4575 const generatedText = ( result as any ) . output [ 0 ] . generated_text as string
46- // the generated text remove a space from the context. that why we need to remove all the spaces
47- const clean = generatedText . replace ( / / g, '' ) . replace ( word . replace ( / / g, '' ) , '' )
48- console . log ( 'suggest result' , clean )
76+ // the generated text remove a space from the context...
77+ const clean = generatedText . replace ( '@custom:dev-run-script' , '@custom:dev-run-script ' ) . replace ( word , '' )
4978 const item : monacoTypes . languages . InlineCompletion = {
5079 insertText : clean
5180 } ;
52-
81+
5382 // abort if there is a signal
5483 if ( token . isCancellationRequested ) {
5584 console . log ( 'aborted' )
@@ -62,17 +91,17 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
6291
6392 }
6493 handleItemDidShow ?( completions : monacoTypes . languages . InlineCompletions < monacoTypes . languages . InlineCompletion > , item : monacoTypes . languages . InlineCompletion , updatedInsertText : string ) : void {
65-
94+
6695 }
6796 handlePartialAccept ?( completions : monacoTypes . languages . InlineCompletions < monacoTypes . languages . InlineCompletion > , item : monacoTypes . languages . InlineCompletion , acceptedCharacters : number ) : void {
6897
6998 }
7099 freeInlineCompletions ( completions : monacoTypes . languages . InlineCompletions < monacoTypes . languages . InlineCompletion > ) : void {
71-
100+
72101 }
73102 groupId ?: string ;
74103 yieldsToGroupIds ?: string [ ] ;
75104 toString ?( ) : string {
76105 throw new Error ( 'Method not implemented.' ) ;
77106 }
78- }
107+ }
0 commit comments