@@ -126,10 +126,31 @@ async function getLocalEmbedding(input: string): Promise<number[]> {
126
126
process . versions != null &&
127
127
process . versions . node != null ;
128
128
129
- if ( isNode ) {
130
- const fs = await import ( "fs" ) ;
131
- const { FlagEmbedding } = await import ( "fastembed" ) ;
132
- const { fileURLToPath } = await import ( "url" ) ;
129
+ if ( ! isNode ) {
130
+ elizaLogger . warn (
131
+ "Local embedding not supported in browser, falling back to remote embedding"
132
+ ) ;
133
+ throw new Error ( "Local embedding not supported in browser" ) ;
134
+ }
135
+
136
+ try {
137
+ // Try to dynamically import all required Node.js modules
138
+ const moduleImports = await Promise . all ( [
139
+ import ( "fs" ) ,
140
+ import ( "url" ) ,
141
+ // Wrap fastembed import in a try-catch to prevent build errors for non-Node.js environments.
142
+ ( async ( ) => {
143
+ try {
144
+ return await import ( "fastembed" ) ;
145
+ } catch ( error ) {
146
+ elizaLogger . error ( "Failed to load fastembed:" , error ) ;
147
+ throw new Error ( "fastembed import failed, falling back to remote embedding" ) ;
148
+ }
149
+ } ) ( )
150
+ ] ) ;
151
+
152
+ const [ fs , { fileURLToPath } , fastEmbed ] = moduleImports ;
153
+ const { FlagEmbedding } = fastEmbed ;
133
154
134
155
function getRootPath ( ) {
135
156
const __filename = fileURLToPath ( import . meta. url ) ;
@@ -156,11 +177,8 @@ async function getLocalEmbedding(input: string): Promise<number[]> {
156
177
const trimmedInput = trimTokens ( input , 8000 , "gpt-4o-mini" ) ;
157
178
const embedding = await embeddingModel . queryEmbed ( trimmedInput ) ;
158
179
return embedding ;
159
- } else {
160
- // Browser implementation - fallback to remote embedding
161
- elizaLogger . warn (
162
- "Local embedding not supported in browser, falling back to remote embedding"
163
- ) ;
180
+ } catch ( error ) {
181
+ elizaLogger . warn ( "Local embedding not supported in browser, falling back to remote embedding:" , error ) ;
164
182
throw new Error ( "Local embedding not supported in browser" ) ;
165
183
}
166
184
}
0 commit comments