forked from chroma-core/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use workaround for dynamic import (chroma-core#956)
resolves chroma-core#953 ## Description of changes - Bug fixes - implement a workaround in clients/js/src/embeddings/WebAIEmbeddingFunction.ts to resolve chroma-core#953 ## Test plan At present, we lack a testing setup specifically tailored for a browser environment. Implementing this would be a necessary step to implement tests for changes in this branch. I've tested it locally using this change: https://github.com/jeffchuber/nextjs-chroma/pull/1/files - http://localhost:3000/api/hello (node version) works fine - http://localhost:3000 (browser version) works fine
- Loading branch information
Showing
5 changed files
with
1,891 additions
and
1,325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,84 @@ | ||
import { Api } from "./generated" | ||
import { Api } from "./generated"; | ||
import Count200Response = Api.Count200Response; | ||
|
||
// a function to convert a non-Array object to an Array | ||
export function toArray<T>(obj: T | Array<T>): Array<T> { | ||
if (Array.isArray(obj)) { | ||
return obj; | ||
} else { | ||
return [obj]; | ||
} | ||
if (Array.isArray(obj)) { | ||
return obj; | ||
} else { | ||
return [obj]; | ||
} | ||
} | ||
|
||
// a function to convert an array to array of arrays | ||
export function toArrayOfArrays<T>(obj: Array<Array<T>> | Array<T>): Array<Array<T>> { | ||
if (Array.isArray(obj[0])) { | ||
return obj as Array<Array<T>>; | ||
} else { | ||
return [obj] as Array<Array<T>>; | ||
} | ||
export function toArrayOfArrays<T>( | ||
obj: Array<Array<T>> | Array<T> | ||
): Array<Array<T>> { | ||
if (Array.isArray(obj[0])) { | ||
return obj as Array<Array<T>>; | ||
} else { | ||
return [obj] as Array<Array<T>>; | ||
} | ||
} | ||
|
||
// we need to override constructors to make it work with jest | ||
// https://stackoverflow.com/questions/76007003/jest-tobeinstanceof-expected-constructor-array-received-constructor-array | ||
export function repack(value: unknown): any { | ||
if (Boolean(value) && typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
return new Array(...value); | ||
} else { | ||
return { ...value }; | ||
} | ||
if (Boolean(value) && typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
return new Array(...value); | ||
} else { | ||
return value; | ||
return { ...value }; | ||
} | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
export async function handleError(error: unknown) { | ||
|
||
if (error instanceof Response) { | ||
try { | ||
const res = await error.json(); | ||
if ("error" in res) { | ||
return { error: res.error }; | ||
} | ||
} catch (e: unknown) { | ||
return { | ||
//@ts-ignore | ||
error: | ||
e && typeof e === "object" && "message" in e | ||
? e.message | ||
: "unknown error", | ||
}; | ||
} | ||
if (error instanceof Response) { | ||
try { | ||
const res = await error.json(); | ||
if ("error" in res) { | ||
return { error: res.error }; | ||
} | ||
} catch (e: unknown) { | ||
return { | ||
//@ts-ignore | ||
error: | ||
e && typeof e === "object" && "message" in e | ||
? e.message | ||
: "unknown error", | ||
}; | ||
} | ||
return { error }; | ||
} | ||
return { error }; | ||
} | ||
|
||
export async function handleSuccess(response: Response | string | Count200Response) { | ||
switch (true) { | ||
case response instanceof Response: | ||
return repack(await (response as Response).json()); | ||
case typeof response === "string": | ||
return repack((response as string)); // currently version is the only thing that return non-JSON | ||
default: | ||
return repack(response); | ||
} | ||
export async function handleSuccess( | ||
response: Response | string | Count200Response | ||
) { | ||
switch (true) { | ||
case response instanceof Response: | ||
return repack(await (response as Response).json()); | ||
case typeof response === "string": | ||
return repack(response as string); // currently version is the only thing that return non-JSON | ||
default: | ||
return repack(response); | ||
} | ||
} | ||
|
||
/** | ||
* Dynamically imports a specified module, providing a workaround for browser environments. | ||
* This function is necessary because we dynamically import optional dependencies | ||
* which can cause issues with bundlers that detect the import and throw an error | ||
* on build time when the dependency is not installed. | ||
* Using this workaround, the dynamic import is only evaluated on runtime | ||
* where we work with try-catch when importing optional dependencies. | ||
* | ||
* @param {string} moduleName - Specifies the module to import. | ||
* @returns {Promise<any>} Returns a Promise that resolves to the imported module. | ||
*/ | ||
export async function importOptionalModule(moduleName: string) { | ||
return Function(`return import("${moduleName}")`)(); | ||
} |
Oops, something went wrong.