@@ -2,29 +2,34 @@ import { AnthropicClient } from './anthropic';
2
2
import { Neovim , NvimPlugin } from 'neovim' ;
3
3
import { Sidebar } from './sidebar' ;
4
4
import { Chat } from './chat' ;
5
+ import { Logger } from './logger'
5
6
6
7
class Magenta {
7
8
private anthropicClient : AnthropicClient ;
8
9
private sidebar : Sidebar ;
9
10
private chat : Chat ;
11
+ private logger : Logger ;
10
12
11
13
constructor ( private nvim : Neovim , plugin : NvimPlugin ) {
12
- console . error ( 'Hello from magenta' )
13
- this . anthropicClient = new AnthropicClient ( ) ;
14
- this . sidebar = new Sidebar ( this . nvim ) ;
15
- this . chat = new Chat ( this . nvim ) ;
14
+ this . logger = new Logger ( this . nvim , { level : 'trace' } ) ;
15
+ this . logger . debug ( `Initializing plugin` )
16
+ this . anthropicClient = new AnthropicClient ( this . logger ) ;
17
+ this . sidebar = new Sidebar ( this . nvim , this . logger ) ;
18
+ this . chat = new Chat ( ) ;
16
19
17
20
plugin . registerCommand ( 'Magenta' , ( args : string [ ] ) => this . command ( args ) , {
18
21
nargs : '1'
19
22
} )
20
23
}
21
24
22
25
async command ( args : string [ ] ) : Promise < void > {
26
+ this . logger . debug ( `Received command ${ args [ 0 ] } ` )
23
27
switch ( args [ 0 ] ) {
24
- case 'toggle' :
28
+ case 'toggle' : {
25
29
const inputBuffer = await this . sidebar . toggle ( ) ;
26
- this . nvim . lua ( `vim.keymap.set('n', '<leader>x', ':Magenta send<CR>', { buffer = ${ inputBuffer . id } })` ) ;
30
+ await this . nvim . lua ( `vim.keymap.set('n', '<leader>x', ':Magenta send<CR>', { buffer = ${ inputBuffer . id } })` ) ;
27
31
break ;
32
+ }
28
33
29
34
case 'send' :
30
35
await this . sendMessage ( ) ;
@@ -35,33 +40,41 @@ class Magenta {
35
40
break ;
36
41
37
42
default :
38
- await this . nvim . outWrite ( `Unrecognized command ${ args [ 0 ] } \n` ) ;
43
+ this . logger . error ( `Unrecognized command ${ args [ 0 ] } \n` ) ;
39
44
}
40
45
}
41
46
42
47
private async sendMessage ( ) {
43
48
const message = await this . sidebar . getMessage ( ) ;
49
+ this . logger . trace ( `current message: ${ message } ` )
44
50
if ( ! message ) return ;
45
51
46
- // Add user message to chat and display
47
52
this . chat . addMessage ( 'user' , message ) ;
48
- await this . sidebar . appendToMain ( {
53
+ this . chat . addMessage ( 'assistant' , '' ) ;
54
+ await this . sidebar . appendToDisplayBuffer ( {
49
55
text : `\nUser: ${ message } \n\nAssistant: ` ,
50
56
scrollTop : false
51
57
} ) ;
52
58
53
- // Stream the assistant's response
54
- this . anthropicClient . sendMessage ( this . chat . getMessages ( ) , ( text ) => {
59
+ this . anthropicClient . sendMessage ( this . chat . getMessages ( ) , async ( text ) => {
60
+ this . logger . trace ( `stream received text ${ text } ` )
55
61
this . chat . appendToCurrentMessage ( text ) ;
56
- this . sidebar . appendToMain ( {
62
+ await this . sidebar . appendToDisplayBuffer ( {
57
63
text,
58
64
scrollTop : false
59
65
} ) ;
60
66
} ) ;
61
67
}
62
68
}
63
69
70
+ let singleton : Magenta | undefined = undefined ;
71
+
64
72
module . exports = ( plugin : NvimPlugin ) => {
65
- console . log ( 'registering plugin' )
66
- new Magenta ( plugin . nvim , plugin )
73
+ plugin . setOptions ( {
74
+ // dev: true,
75
+ // alwaysInit: true
76
+ } )
77
+ if ( ! singleton ) {
78
+ singleton = new Magenta ( plugin . nvim , plugin )
79
+ }
67
80
}
0 commit comments