Skip to content

Commit 90f5676

Browse files
committed
wrap in try to allow non node env to build
1 parent 379aeb3 commit 90f5676

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

packages/core/src/embedding.ts

+27-9
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,31 @@ async function getLocalEmbedding(input: string): Promise<number[]> {
126126
process.versions != null &&
127127
process.versions.node != null;
128128

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;
133154

134155
function getRootPath() {
135156
const __filename = fileURLToPath(import.meta.url);
@@ -156,11 +177,8 @@ async function getLocalEmbedding(input: string): Promise<number[]> {
156177
const trimmedInput = trimTokens(input, 8000, "gpt-4o-mini");
157178
const embedding = await embeddingModel.queryEmbed(trimmedInput);
158179
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);
164182
throw new Error("Local embedding not supported in browser");
165183
}
166184
}

0 commit comments

Comments
 (0)