@@ -18,6 +18,7 @@ import { getInnerLibs } from './lib';
1818import { getOutputChannel } from './outputChannel' ;
1919import { getLastAcitveTextEditor } from '../context' ;
2020import { getSyncFolder } from './config' ;
21+ import { createChatCompletionForScript } from './openai' ;
2122
2223export const genCodeByBlock = async ( data : {
2324 material : string ;
@@ -58,12 +59,14 @@ export const genCodeByBlock = async (data: {
5859 }
5960 } ) ;
6061 }
61- const scriptFile = path . join ( tempWorkPath , 'script/index.js' ) ;
62+ const scriptFile = path . join ( block , 'script/index.js' ) ; // 不能使用临时目录里的文件,会导致 ts-node 报错
6263 const hook = {
6364 beforeCompile : ( context : any ) =>
6465 < object | undefined > Promise . resolve ( undefined ) ,
6566 afterCompile : ( context : any ) =>
6667 < object | undefined > Promise . resolve ( undefined ) ,
68+ complete : ( context : any ) =>
69+ < object | undefined > Promise . resolve ( undefined ) ,
6770 } ;
6871 if ( fs . existsSync ( scriptFile ) ) {
6972 delete eval ( 'require' ) . cache [ eval ( 'require' ) . resolve ( scriptFile ) ] ;
@@ -74,6 +77,9 @@ export const genCodeByBlock = async (data: {
7477 if ( script . afterCompile ) {
7578 hook . afterCompile = script . afterCompile ;
7679 }
80+ if ( script . complete ) {
81+ hook . complete = script . complete ;
82+ }
7783 }
7884 const context = {
7985 model : data . model ,
@@ -82,6 +88,11 @@ export const genCodeByBlock = async (data: {
8288 env : getEnv ( ) ,
8389 libs : getInnerLibs ( ) ,
8490 outputChannel : getOutputChannel ( ) ,
91+ log : getOutputChannel ( ) ,
92+ createBlockPath : path
93+ . join ( data . path , ...data . createPath )
94+ . replace ( / \\ / g, '/' ) ,
95+ createChatCompletion : createChatCompletionForScript ,
8596 } ;
8697 data . model = {
8798 ...data . model ,
@@ -106,6 +117,7 @@ export const genCodeByBlock = async (data: {
106117 path . join ( tempWorkPath , 'src' ) ,
107118 path . join ( data . path , ...data . createPath ) ,
108119 ) ;
120+ await hook . complete ( context ) ;
109121 fs . removeSync ( tempWorkPath ) ;
110122 } catch ( ex : any ) {
111123 fs . remove ( tempWorkPath ) ;
@@ -182,6 +194,8 @@ export const genCodeBySnippet = async (data: {
182194 env : getEnv ( ) ,
183195 libs : getInnerLibs ( ) ,
184196 outputChannel : getOutputChannel ( ) ,
197+ log : getOutputChannel ( ) ,
198+ createChatCompletion : createChatCompletionForScript ,
185199 code : '' ,
186200 } ;
187201 const extendModel = await hook . beforeCompile ( context ) ;
0 commit comments