@@ -4,9 +4,6 @@ import { ServiceContainer } from './services/ServiceContainer';
44
55const reconnectString : string = "%connect%"
66const saveString : string = "%save%" ;
7- const bootFileName : string = "main.py" ;
8- const bootSavePrefix : string = `with open('${ bootFileName } ', 'w') as f:\n f.write('''\n` ;
9- const bootSaveSuffix : string = "\n''')\n" ;
107
118export class EmbeddedKernel extends BaseKernel {
129
@@ -83,53 +80,11 @@ export class EmbeddedKernel extends BaseKernel {
8380 }
8481 if ( code . includes ( saveString ) ) {
8582 // Burn the code after the save string into the main.py file
86- this . outputResponse ( `Save command detected, saving code to ${ bootFileName } ...` ) ;
83+ this . outputResponse ( `Save command detected, saving code...` ) ;
8784 const codeToSave = code . split ( saveString ) [ 1 ] . trim ( ) ;
88- if ( ! codeToSave ) {
89- return {
90- status : 'error' ,
91- execution_count : this . executionCount ,
92- ename : 'ValueError' ,
93- evalue : 'No code provided to save' ,
94- traceback : [ 'Please provide code to save after the %save% command' ]
95- } ;
96- }
97- // Burn the code by adding it to the main.py file. Can call executeRequest with the code to save
98- // but append the code to write it to the main.py file at the start of the code.
99- const bootCode = `# This code is automatically generated by the Embedded Kernel\n${ codeToSave } ` ;
100- const bootCodeWithSave = `${ bootSavePrefix } ${ bootCode } ${ bootSaveSuffix } ` ;
101-
102- // Execute the command to save the main.py file
103- if ( ! this . serviceContainer . deviceService . getTransport ( ) ) {
104- console . log ( `[Kernel] executeRequest - No transport available for saving ${ bootFileName } ` ) ;
105- return {
106- status : 'error' ,
107- execution_count : this . executionCount ,
108- ename : 'TransportError' ,
109- evalue : `No transport available to save ${ bootFileName } ` ,
110- traceback : [ 'Please connect a device first' ]
111- } ;
112- }
113- const result = await this . serviceContainer . consoleService . executeCommand ( bootCodeWithSave , ( content ) => {
114- console . log ( "[Kernel] executeRequest - Streaming output:" , content ) ;
115- this . stream ( content ) ;
116- } ) ;
117- if ( ! result . success ) {
118- console . log ( "[Kernel] executeRequest - Command execution failed:" , result . error ) ;
119- return {
120- status : 'error' ,
121- execution_count : this . executionCount ,
122- ename : 'ExecutionError' ,
123- evalue : result . error || 'Unknown error' ,
124- traceback : [ result . error || 'Unknown error' ]
125- } ;
126- }
127- console . log ( `[Kernel] executeRequest - Code saved to ${ bootFileName } successfully` ) ;
128- return {
129- status : 'ok' ,
130- execution_count : this . executionCount ,
131- user_expressions : { } ,
132- } ;
85+ // pass the "stream" to the saveCodeToDevice method
86+ const result = await this . serviceContainer . saveCodeToDevice ( codeToSave , this . outputResponse . bind ( this ) ) ;
87+ return result ;
13388 }
13489
13590 try {
0 commit comments