@@ -13,6 +13,46 @@ const client = createDockerDesktopClient();
13
13
const track = ( event : string ) =>
14
14
client . extension . vm ?. service ?. post ( '/analytics/track' , { event } ) ;
15
15
16
+ class OutputParser {
17
+ output : any [ ] = [ ] ;
18
+ callback : ( output : any [ ] ) => void ;
19
+ constructor ( callback : ( output : any [ ] ) => void ) {
20
+ this . output = [ ] ;
21
+ this . callback = callback ;
22
+ }
23
+ updateOutput = ( line : any ) => {
24
+ let output = [ ...this . output ] ;
25
+ if ( line . method === 'functions' ) {
26
+ const functions = line . params ;
27
+ for ( const func of functions ) {
28
+ const functionId = func . id ;
29
+ const existingFunction = output . find ( o =>
30
+ o . method === 'functions'
31
+ &&
32
+ o . params . find ( ( p : { id : string } ) => p . id === functionId )
33
+ ) ;
34
+ if ( existingFunction ) {
35
+ const existingFunctionParamsIndex = existingFunction . params . findIndex ( ( p : { id : string } ) => p . id === functionId ) ;
36
+ existingFunction . params [ existingFunctionParamsIndex ] = { ...existingFunction . params [ existingFunctionParamsIndex ] , ...func } ;
37
+ output = output . map (
38
+ o => o . method === 'functions'
39
+ ?
40
+ { ...o , params : o . params . map ( ( p : { id : string } ) => p . id === functionId ? { ...p , ...func } : p ) }
41
+ :
42
+ o
43
+ ) ;
44
+ } else {
45
+ output = [ ...output , line ] ;
46
+ }
47
+ }
48
+ }
49
+ else {
50
+ output = [ ...output , line ] ;
51
+ }
52
+ this . callback ( output ) ;
53
+ }
54
+ }
55
+
16
56
const debounce = ( fn : Function , ms : number ) => {
17
57
let timeout : NodeJS . Timeout ;
18
58
return function ( ...args : any ) {
@@ -40,6 +80,39 @@ export function App() {
40
80
41
81
const [ showDebug , setShowDebug ] = React . useState ( false ) ;
42
82
83
+ useEffect ( ( ) => {
84
+ const runOutput = new OutputParser ( setRunOut ) ;
85
+ try {
86
+ client . docker . cli . exec ( "pull" , [ "vonwig/function_write_files" ] ) . then ( ( ) => {
87
+ client . docker . cli . exec ( "run" , [
88
+ "-v" ,
89
+ "openai_key:/root" ,
90
+ "--workdir" , "/root" ,
91
+ "vonwig/function_write_files" ,
92
+ `'` + JSON . stringify ( { files : [ { path : ".openai-api-key" , content : openAIKey , executable : false } ] } ) + `'`
93
+ ] ) ;
94
+ } ) ;
95
+ client . docker . cli . exec ( "pull" , [ "vonwig/prompts" ] , {
96
+ stream : {
97
+ onOutput : ( { stdout, stderr } ) => {
98
+ if ( stdout ) {
99
+ runOutput . updateOutput ( { method : 'message' , params : { debug : stdout } } ) ;
100
+ }
101
+ if ( stderr ) {
102
+ runOutput . updateOutput ( { method : 'error' , params : { content : stderr } } ) ;
103
+ }
104
+ } ,
105
+ onError : ( err ) => {
106
+ runOutput . updateOutput ( { method : 'error' , params : { content : err } } ) ;
107
+ } ,
108
+ } ,
109
+ } ) ;
110
+ }
111
+ catch ( e ) {
112
+ runOutput . updateOutput ( { method : 'message' , params : { debug : JSON . stringify ( e ) } } ) ;
113
+ }
114
+ } , [ ] ) ;
115
+
43
116
useEffect ( ( ) => {
44
117
localStorage . setItem ( 'projects' , JSON . stringify ( projects ) ) ;
45
118
if ( ! selectedProject && projects . length > 0 ) {
@@ -94,54 +167,11 @@ export function App() {
94
167
95
168
const startPrompt = async ( ) => {
96
169
track ( 'start-prompt' ) ;
97
- let output : any [ ] = [ ]
98
- const updateOutput = ( line : any ) => {
99
- if ( line . method === 'functions' ) {
100
- const functions = line . params ;
101
- for ( const func of functions ) {
102
- const functionId = func . id ;
103
- const existingFunction = output . find ( o =>
104
- o . method === 'functions'
105
- &&
106
- o . params . find ( ( p : { id : string } ) => p . id === functionId )
107
- ) ;
108
- if ( existingFunction ) {
109
- const existingFunctionParamsIndex = existingFunction . params . findIndex ( ( p : { id : string } ) => p . id === functionId ) ;
110
- existingFunction . params [ existingFunctionParamsIndex ] = { ...existingFunction . params [ existingFunctionParamsIndex ] , ...func } ;
111
- output = output . map (
112
- o => o . method === 'functions'
113
- ?
114
- { ...o , params : o . params . map ( ( p : { id : string } ) => p . id === functionId ? { ...p , ...func } : p ) }
115
- :
116
- o
117
- ) ;
118
- } else {
119
- output = [ ...output , line ] ;
120
- }
121
- }
122
- }
123
- else {
124
- output = [ ...output , line ] ;
125
- }
126
- setRunOut ( output ) ;
127
- }
128
- updateOutput ( { method : 'message' , params : { debug : 'Pulling images' } } )
129
- try {
130
- const pullWriteFiles = await client . docker . cli . exec ( "pull" , [ "vonwig/function_write_files" ] ) ;
131
- const pullPrompts = await client . docker . cli . exec ( "pull" , [ "vonwig/prompts" ] ) ;
132
- const writeKey = await client . docker . cli . exec ( "run" , [
133
- "-v" ,
134
- "openai_key:/root" ,
135
- "--workdir" , "/root" ,
136
- "vonwig/function_write_files" ,
137
- `'` + JSON . stringify ( { files : [ { path : ".openai-api-key" , content : openAIKey , executable : false } ] } ) + `'`
138
- ] ) ;
139
- updateOutput ( { method : 'message' , params : { debug : JSON . stringify ( { pullWriteFiles, pullPrompts, writeKey } ) } } ) ;
140
- }
141
- catch ( e ) {
142
- updateOutput ( { method : 'message' , params : { debug : JSON . stringify ( e ) } } ) ;
143
- }
144
- updateOutput ( { method : 'message' , params : { debug : 'Running prompts...' } } )
170
+
171
+ const runOutput = new OutputParser ( setRunOut ) ;
172
+ runOutput . updateOutput ( { method : 'message' , params : { debug : 'Pulling images' } } )
173
+
174
+ runOutput . updateOutput ( { method : 'message' , params : { debug : 'Running prompts...' } } )
145
175
const args = getRunArgs ( selectedPrompt ! , selectedProject ! , "" , client . host . platform )
146
176
147
177
client . docker . cli . exec ( "run" , args , {
@@ -154,15 +184,15 @@ export function App() {
154
184
rpcMessage += '}'
155
185
}
156
186
const json = JSON . parse ( rpcMessage )
157
- updateOutput ( json )
187
+ runOutput . updateOutput ( json )
158
188
}
159
189
if ( stderr ) {
160
- updateOutput ( { method : 'message' , params : { debug : stderr } } ) ;
190
+ runOutput . updateOutput ( { method : 'message' , params : { debug : stderr } } ) ;
161
191
}
162
192
} ,
163
193
onError : ( err ) => {
164
194
console . error ( err ) ;
165
- updateOutput ( { method : 'message' , params : { debug : err } } ) ;
195
+ runOutput . updateOutput ( { method : 'message' , params : { debug : err } } ) ;
166
196
} ,
167
197
}
168
198
} ) ;
0 commit comments