@@ -3,34 +3,32 @@ import { NvimPlugin } from "neovim";
3
3
import { Sidebar } from "./sidebar.ts" ;
4
4
import * as Chat from "./chat/chat.ts" ;
5
5
import { Logger } from "./logger.ts" ;
6
- import { Context } from "./types.ts" ;
7
6
import { App , createApp } from "./tea/tea.ts" ;
8
7
import * as ToolManager from "./tools/toolManager.ts" ;
9
8
import { d } from "./tea/view.ts" ;
9
+ import { setContext , context } from "./context.ts" ;
10
10
11
11
class Magenta {
12
12
private anthropicClient : AnthropicClient ;
13
13
private sidebar : Sidebar ;
14
14
private chat : App < Chat . Msg , Chat . Model > ;
15
15
private toolManager : App < ToolManager . Msg , ToolManager . Model > ;
16
16
17
- constructor ( private context : Context ) {
18
- this . context . logger . debug ( `Initializing plugin` ) ;
19
- this . anthropicClient = new AnthropicClient ( this . context . logger ) ;
20
- this . sidebar = new Sidebar ( this . context . nvim , this . context . logger ) ;
17
+ constructor ( ) {
18
+ context . logger . debug ( `Initializing plugin` ) ;
19
+ this . anthropicClient = new AnthropicClient ( ) ;
20
+ this . sidebar = new Sidebar ( ) ;
21
21
22
22
this . chat = createApp ( {
23
23
initialModel : { messages : [ ] } ,
24
24
update : Chat . update ,
25
25
View : Chat . view ,
26
- context,
27
26
} ) ;
28
27
29
28
this . toolManager = createApp ( {
30
29
initialModel : ToolManager . initModel ( ) ,
31
30
update : ToolManager . update ,
32
31
View : ( ) => d `` ,
33
- context,
34
32
onUpdate : ( msg , model ) => {
35
33
if ( msg . type == "tool-msg" ) {
36
34
if ( msg . msg . msg . type == "finish" ) {
@@ -53,7 +51,7 @@ class Magenta {
53
51
54
52
if ( shouldRespond ) {
55
53
this . sendMessage ( ) . catch ( ( err ) =>
56
- this . context . logger . error ( err as Error ) ,
54
+ context . logger . error ( err as Error ) ,
57
55
) ;
58
56
}
59
57
}
@@ -64,26 +62,25 @@ class Magenta {
64
62
}
65
63
66
64
async command ( args : string [ ] ) : Promise < void > {
67
- this . context . logger . debug ( `Received command ${ args [ 0 ] } ` ) ;
65
+ context . logger . debug ( `Received command ${ args [ 0 ] } ` ) ;
68
66
switch ( args [ 0 ] ) {
69
67
case "toggle" : {
70
68
const buffers = await this . sidebar . toggle ( ) ;
71
69
if ( buffers ) {
72
70
await this . chat . mount ( {
73
- nvim : this . context . nvim ,
74
71
buffer : buffers . displayBuffer ,
75
72
startPos : { row : 0 , col : 0 } ,
76
73
endPos : { row : 0 , col : 0 } ,
77
74
} ) ;
78
- this . context . logger . trace ( `Chat rendered.` ) ;
75
+ context . logger . trace ( `Chat rendered.` ) ;
79
76
}
80
77
81
78
break ;
82
79
}
83
80
84
81
case "send" : {
85
82
const message = await this . sidebar . getMessage ( ) ;
86
- this . context . logger . trace ( `current message: ${ message } ` ) ;
83
+ context . logger . trace ( `current message: ${ message } ` ) ;
87
84
if ( ! message ) return ;
88
85
89
86
this . chat . dispatch ( {
@@ -101,14 +98,14 @@ class Magenta {
101
98
break ;
102
99
103
100
default :
104
- this . context . logger . error ( `Unrecognized command ${ args [ 0 ] } \n` ) ;
101
+ context . logger . error ( `Unrecognized command ${ args [ 0 ] } \n` ) ;
105
102
}
106
103
}
107
104
108
105
private async sendMessage ( ) {
109
106
const state = this . chat . getState ( ) ;
110
107
if ( state . status != "running" ) {
111
- this . context . logger . error ( `chat is not running.` ) ;
108
+ context . logger . error ( `chat is not running.` ) ;
112
109
return ;
113
110
}
114
111
@@ -117,7 +114,7 @@ class Magenta {
117
114
const toolRequests = await this . anthropicClient . sendMessage (
118
115
messages ,
119
116
( text ) => {
120
- this . context . logger . trace ( `stream received text ${ text } ` ) ;
117
+ context . logger . trace ( `stream received text ${ text } ` ) ;
121
118
this . chat . dispatch ( {
122
119
type : "stream-response" ,
123
120
text,
@@ -148,13 +145,18 @@ module.exports = (plugin: NvimPlugin) => {
148
145
149
146
if ( ! init ) {
150
147
const logger = new Logger ( plugin . nvim , { level : "trace" } ) ;
148
+ setContext ( {
149
+ nvim : plugin . nvim ,
150
+ logger,
151
+ } ) ;
152
+
151
153
process . on ( "uncaughtException" , ( error ) => {
152
154
logger . error ( error ) ;
153
155
process . exit ( 1 ) ;
154
156
} ) ;
155
157
156
158
init = {
157
- magenta : new Magenta ( { nvim : plugin . nvim , logger } ) ,
159
+ magenta : new Magenta ( ) ,
158
160
logger,
159
161
} ;
160
162
0 commit comments