1+ import debug from "debug"
2+ const dbg = debug ( "genaiscript:importprompt" )
3+
14import { host } from "./host"
25import { logError } from "./util"
36import { TraceOptions } from "./trace"
@@ -12,10 +15,13 @@ export async function importFile<T = void>(
1215 } & TraceOptions
1316) : Promise < T > {
1417 const { trace, onImported } = options || { }
15- if ( ! filename ) throw new Error ( "filename is required" )
18+ if ( ! filename ) {
19+ throw new Error ( "filename is required" )
20+ }
1621
1722 let unregister : ( ) => void = undefined
1823 try {
24+ dbg ( `resolving module path for filename: ${ filename } ` )
1925 const modulePath = pathToFileURL (
2026 host . path . isAbsolute ( filename )
2127 ? filename
@@ -25,6 +31,7 @@ export async function importFile<T = void>(
2531 import . meta. url ??
2632 pathToFileURL ( __filename ?? host . projectFolder ( ) ) . toString ( )
2733
34+ dbg ( `importing module from path: ${ modulePath } ` )
2835 const onImport = ( file : string ) => {
2936 // trace?.itemValue("📦 import", fileURLToPath(file))
3037 }
@@ -41,6 +48,7 @@ export async function importFile<T = void>(
4148
4249 return result
4350 } catch ( err ) {
51+ dbg ( "module imported failed" )
4452 unregister ?.( )
4553 logError ( err )
4654 trace ?. error ( err )
@@ -57,15 +65,19 @@ export async function importPrompt(
5765) {
5866 mark ( "prompt.import" )
5967 const { filename } = r
68+ dbg ( `importing file: ${ filename } ` )
6069 return await importFile ( filename , {
6170 ...( options || { } ) ,
6271 onImported : async ( module ) => {
6372 const main = module . default
64- if ( typeof main === "function" ) await main ( ctx0 )
65- else if ( r . isSystem )
73+ if ( typeof main === "function" ) {
74+ dbg ( `found default export as function, calling` )
75+ await main ( ctx0 )
76+ } else if ( r . isSystem ) {
6677 throw new Error (
6778 "system prompt using esm JavaScript (mjs, mts) must have a default function."
6879 )
80+ }
6981 } ,
7082 } )
7183}
0 commit comments