1
1
import * as vscode from 'vscode'
2
- import { correct , getPrompt } from 'utils-ai'
3
- import { getOpenAIKey } from '../secrets'
2
+ import type { Language } from 'utils-ai'
3
+ import { Correcter , CorrecterOptions , FetcherOptions , HttpFetcher , Prompter , PrompterOptions , SimpleMessagesFactory , SimpleSplitter , SimpleTokenizer } from 'utils-ai'
4
+ import { Configurator } from '../configurator.js'
5
+ import { Logger } from '../logger.js'
6
+ import { SecretsStorage } from '../secrets_storage.js'
4
7
5
8
export function correctCommand ( context : vscode . ExtensionContext ) {
6
- const secrets = context . secrets
9
+ const logger = new Logger (
10
+ vscode . window ,
11
+ )
12
+
13
+ const secretsStorage = new SecretsStorage (
14
+ context . secrets ,
15
+ )
7
16
8
17
return async ( ) => {
9
- const accessKey = await getOpenAIKey ( secrets )
18
+ logger . log ( 'Correct command called.' )
19
+
20
+ const configurator = new Configurator (
21
+ vscode . workspace ,
22
+ )
10
23
11
- if ( ! accessKey ) {
12
- vscode . window . showErrorMessage ( 'No key provided. Please provide a key using the "Add OpenAI Key" command.' )
24
+ const authToken = await secretsStorage . getAuthToken ( )
25
+
26
+ if ( ! authToken ) {
27
+ const message = 'No authorization token provided. Please provide an authorization token using the "Add Auth Token" command.'
28
+ logger . log ( message )
29
+ vscode . window . showErrorMessage ( message )
13
30
return
14
31
}
15
32
33
+ const preferredLanguage = configurator . alwaysAskLanguage
34
+ ? await vscode . window . showQuickPick ( Prompter . LANGUAGES ) as Language | undefined || configurator . preferredLanguage
35
+ : configurator . preferredLanguage
36
+
16
37
const time = Date . now ( )
17
38
const editor = vscode . window . activeTextEditor
18
39
19
40
if ( editor ) {
20
41
const editorFilename = editor . document . fileName // Full path to the file
42
+ logger . log ( `File used: ${ editorFilename } ` )
21
43
const filename = editorFilename . split ( '/' ) . pop ( )
22
44
23
45
// Commit current file to git before correcting. This is useful for tracking changes.
24
46
const gitExtension = vscode . extensions . getExtension ( 'vscode.git' ) ?. exports
25
47
const isActivated = gitExtension . enabled
26
48
27
49
if ( ! isActivated ) {
28
- vscode . window . showErrorMessage ( 'Git is not activated. Please activate Git in VSCode.' )
50
+ const message = 'Git is not activated. Please activate Git in VSCode.'
51
+ logger . log ( message )
52
+ vscode . window . showErrorMessage ( message )
29
53
return
30
54
}
31
55
@@ -35,6 +59,7 @@ export function correctCommand(context: vscode.ExtensionContext) {
35
59
if ( git . repositories . length > 0 ) {
36
60
const repository = git . repositories [ 0 ]
37
61
62
+ logger . log ( `Committing ${ filename } ...` )
38
63
vscode . window . showInformationMessage ( `Committing ${ filename } ...` )
39
64
40
65
// Save to be sure to commit the latest changes
@@ -45,12 +70,18 @@ export function correctCommand(context: vscode.ExtensionContext) {
45
70
await repository . commit ( `chore: save ${ filename } before correcting` )
46
71
}
47
72
catch ( error : any ) {
73
+ logger . log ( 'No changes to commit.' )
48
74
// Ignore error because it's mean that the file is already committed
49
75
}
50
76
}
51
77
}
52
78
53
- const prompt = getPrompt ( 'spell-checker-md' , 'en' )
79
+ const prompterOptions = new PrompterOptions (
80
+ preferredLanguage ,
81
+ )
82
+ const prompter = new Prompter ( prompterOptions )
83
+ prompter . merge ( configurator . prompts )
84
+ const prompt = prompter . find ( 'spell-checker' )
54
85
55
86
const hasSelection = ! editor . selection . isEmpty
56
87
const selection = editor . selection
@@ -69,7 +100,28 @@ export function correctCommand(context: vscode.ExtensionContext) {
69
100
title,
70
101
cancellable : false ,
71
102
} , async ( ) => {
72
- const correctedText = await correct ( text , prompt . message , { ai : { accessKey } } )
103
+ const messagesFactory = new SimpleMessagesFactory ( )
104
+
105
+ const tokenizer = new SimpleTokenizer ( )
106
+ const splitter = new SimpleSplitter ( tokenizer )
107
+
108
+ const fetcherOptions = new FetcherOptions (
109
+ authToken ,
110
+ configurator . endpoint ,
111
+ configurator . model ,
112
+ )
113
+ const fetcher = new HttpFetcher ( fetcherOptions )
114
+
115
+ const correcterOption = new CorrecterOptions (
116
+ prompt . message ,
117
+ // Smaller than the context window.
118
+ configurator . outputTokens ,
119
+ )
120
+ const corrector = new Correcter ( messagesFactory , tokenizer , splitter , fetcher , correcterOption )
121
+
122
+ logger . log ( `Correcting ${ hasSelection ? 'selection' : 'selection' } with prompt: ${ prompt . message } ` )
123
+ logger . log ( `Output tokens: ${ configurator . outputTokens } ` )
124
+ const correctedText = await corrector . execute ( text )
73
125
74
126
editor . edit ( ( editBuilder ) => {
75
127
const range : vscode . Range = hasSelection ? selection : new vscode . Range ( 0 , 0 , text . length , 0 )
@@ -86,9 +138,11 @@ export function correctCommand(context: vscode.ExtensionContext) {
86
138
vscode . window . showInformationMessage ( message , {
87
139
detail : `Done in ${ duration } ms.` ,
88
140
} )
141
+ logger . log ( `Correction done in ${ duration } ms.` )
89
142
} )
90
143
}
91
144
catch ( error : any ) {
145
+ logger . log ( error . message )
92
146
vscode . window . showErrorMessage ( error . message )
93
147
}
94
148
}
0 commit comments