@@ -10,6 +10,8 @@ import { deleteCredentials } from "./creds";
10
10
import readline from "readline" ;
11
11
import { findPlugin , executePlugin , initializePlugins } from "./commands" ;
12
12
import determinePlugins from "./rag" ;
13
+ import multiline from "./multiline" ;
14
+ import { clearContext } from "./context" ;
13
15
14
16
const program = new Command ( ) ;
15
17
@@ -26,69 +28,120 @@ program
26
28
// Initialize plugins
27
29
initializePlugins ( ) ;
28
30
29
- const prompt = ( ) => {
31
+ const prompt = async ( ) => {
30
32
process . stdout . write ( chalk . blueBright ( "\nYou: " ) ) ;
31
33
process . stdin . resume ( ) ;
32
34
process . stdin . setEncoding ( "utf-8" ) ;
33
- process . stdin . once ( "data" , async ( data ) => {
34
- const userInput = data . toString ( ) . trim ( ) ;
35
-
36
- if ( creds . apiKey != null ) {
37
- try {
38
- // Direct plugin call
39
- const plugin = findPlugin ( userInput ) ;
40
- if ( plugin ) {
41
- console . log ( chalk . yellow ( `Executing plugin: ${ plugin . name } ` ) ) ;
42
- await executePlugin ( plugin , {
43
- userInput,
44
- engine : creds . engine ,
45
- apiKey : creds . apiKey ,
46
- opts : { ...opts , model : creds . model || undefined } ,
47
- } ) ;
48
- } else {
49
- // Use LLM to determine if a plugin should be used
50
- const pluginKeyword = await determinePlugins (
51
- creds . engine ,
52
- creds . apiKey ,
53
- userInput ,
54
- { ...opts , model : creds . model || undefined }
55
- ) ;
56
-
57
- if ( pluginKeyword !== "none" ) {
58
- const plugin = findPlugin ( pluginKeyword ) ;
59
- if ( plugin ) {
60
- console . log ( chalk . yellow ( `Executing plugin: ${ plugin . name } ` ) ) ;
61
- await executePlugin ( plugin , {
62
- userInput,
63
- engine : creds . engine ,
64
- apiKey : creds . apiKey ,
65
- opts : { ...opts , model : creds . model || undefined } ,
66
- } ) ;
67
- } else {
68
- console . log ( chalk . red ( `Plugin not found: ${ pluginKeyword } ` ) ) ;
69
- }
70
- } else {
71
- // No plugin applicable, use regular promptResponse
72
- await promptResponse ( creds . engine , creds . apiKey , userInput , {
73
- ...opts ,
74
- model : creds . model || undefined ,
35
+
36
+ const data = await multiline ( ) ;
37
+
38
+ // process.stdin.once("data", async (data) => {
39
+ const userInput = data . toString ( ) . trim ( ) ;
40
+
41
+ if ( creds . apiKey != null ) {
42
+ try {
43
+ // Direct plugin call
44
+ const plugin = findPlugin ( userInput ) ;
45
+ if ( plugin ) {
46
+ console . log ( chalk . yellow ( `Executing plugin: ${ plugin . name } ` ) ) ;
47
+ await executePlugin ( plugin , {
48
+ userInput,
49
+ engine : creds . engine ,
50
+ apiKey : creds . apiKey ,
51
+ opts : { ...opts , model : creds . model || undefined } ,
52
+ } ) ;
53
+ } else {
54
+ // Use LLM to determine if a plugin should be used
55
+ const pluginKeyword = await determinePlugins (
56
+ creds . engine ,
57
+ creds . apiKey ,
58
+ userInput ,
59
+ { ...opts , model : creds . model || undefined }
60
+ ) ;
61
+ clearContext ( ) ;
62
+ if ( pluginKeyword . trim ( ) !== "none" ) {
63
+ const plugin = findPlugin ( pluginKeyword ) ;
64
+ if ( plugin ) {
65
+ console . log ( chalk . yellow ( `Executing plugin: ${ plugin . name } ` ) ) ;
66
+ await executePlugin ( plugin , {
67
+ userInput,
68
+ engine : creds . engine ,
69
+ apiKey : creds . apiKey ,
70
+ opts : { ...opts , model : creds . model || undefined } ,
75
71
} ) ;
72
+ } else {
73
+ console . log ( chalk . red ( `Plugin not found: ${ pluginKeyword } ` ) ) ;
76
74
}
75
+ } else {
76
+ await promptResponse ( creds . engine , creds . apiKey , userInput , {
77
+ ...opts ,
78
+ model : creds . model || undefined ,
79
+ } ) ;
77
80
}
78
- } catch ( error ) {
79
- console . error ( chalk . red ( "An error occurred:" ) , error ) ;
80
81
}
81
- } else {
82
- console . log ( chalk . red ( "API key is required for chat functionality." ) ) ;
82
+ } catch ( error ) {
83
+ console . error ( chalk . red ( "An error occurred:" ) , error ) ;
83
84
}
85
+ } else {
86
+ console . log ( chalk . red ( "API key is required for chat functionality." ) ) ;
87
+ }
84
88
85
- prompt ( ) ;
86
- } ) ;
89
+ prompt ( ) ;
90
+ // });
87
91
} ;
88
92
89
93
prompt ( ) ;
90
94
} ) ;
91
95
96
+ program
97
+ . command ( "one-shot <question>" )
98
+ . description ( "Ask a one-shot question and get a quick answer" )
99
+ . option ( "-e, --engine <engine>" , "LLM to use" )
100
+ . option ( "-t, --temperature <temperature>" , "Response temperature" )
101
+ . action ( async ( question , opts ) => {
102
+ await checkIsLatestVersion ( ) ;
103
+ const creds = await apiKeyPrompt ( ) ;
104
+
105
+ if ( creds . apiKey != null ) {
106
+ try {
107
+ // Use LLM to determine if a plugin should be used
108
+ const pluginKeyword = await determinePlugins (
109
+ creds . engine ,
110
+ creds . apiKey ,
111
+ question ,
112
+ { ...opts , model : creds . model || undefined }
113
+ ) ;
114
+
115
+ if ( pluginKeyword . trim ( ) !== "none" ) {
116
+ const plugin = findPlugin ( pluginKeyword ) ;
117
+ if ( plugin ) {
118
+ console . log ( chalk . yellow ( `Executing plugin: ${ plugin . name } ` ) ) ;
119
+ await executePlugin ( plugin , {
120
+ userInput : question ,
121
+ engine : creds . engine ,
122
+ apiKey : creds . apiKey ,
123
+ opts : { ...opts , model : creds . model || undefined } ,
124
+ } ) ;
125
+ } else {
126
+ console . log ( chalk . red ( `Plugin not found: ${ pluginKeyword } ` ) ) ;
127
+ }
128
+ } else {
129
+ // No plugin applicable, use regular promptResponse
130
+ await promptResponse ( creds . engine , creds . apiKey , question , {
131
+ ...opts ,
132
+ model : creds . model || undefined ,
133
+ } ) ;
134
+ }
135
+ } catch ( error ) {
136
+ console . error ( chalk . red ( "An error occurred:" ) , error ) ;
137
+ }
138
+ } else {
139
+ console . log ( chalk . red ( "API key is required for chat functionality." ) ) ;
140
+ }
141
+
142
+ process . exit ( 0 ) ;
143
+ } ) ;
144
+
92
145
program
93
146
. command ( "delete" )
94
147
. description ( "Delete your API key" )
0 commit comments