diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 2e275805e29917f198f48aa4258ffca3c81284a0..f967a188f29a9ef664213abf257fd95bcd22a86d 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -1,4 +1,4 @@ -import { createHash } from "node:crypto"; +import CryptoJS from "crypto-js"; import path from "path"; import { v4 as uuidv4 } from "uuid"; @@ -141,13 +141,11 @@ export abstract class BaseNode<T extends Metadata = Metadata> { } /** - * Creates a deep-clone of the node as JSON + * Used with built in JSON.stringify * @returns */ toJSON(): Record<string, any> { - const json: Record<string, any> = structuredClone(this); - json.type = this.getType(); - return json; + return { ...this, type: this.getType() }; } } @@ -179,13 +177,13 @@ export class TextNode<T extends Metadata = Metadata> extends BaseNode<T> { * @returns */ generateHash() { - const hashFunction = createHash("sha256"); + const hashFunction = CryptoJS.algo.SHA256.create(); hashFunction.update(`type=${this.getType()}`); hashFunction.update( `startCharIdx=${this.startCharIdx} endCharIdx=${this.endCharIdx}`, ); hashFunction.update(this.getContent(MetadataMode.ALL)); - return hashFunction.digest("base64"); + return hashFunction.finalize().toString(CryptoJS.enc.Base64); } getType(): ObjectType { diff --git a/packages/core/src/storage/vectorStore/utils.ts b/packages/core/src/storage/vectorStore/utils.ts index 1c69ae57ef5ed4ca334350aa574aff531dce10a3..cf9e5728e97cc4b00f4433e7acb4224061e0759f 100644 --- a/packages/core/src/storage/vectorStore/utils.ts +++ b/packages/core/src/storage/vectorStore/utils.ts @@ -1,3 +1,4 @@ +import _ from "lodash"; import { BaseNode, jsonToNode, Metadata, ObjectType } from "../../Node"; const DEFAULT_TEXT_KEY = "text"; @@ -16,7 +17,7 @@ export function nodeToMetadata( textField: string = DEFAULT_TEXT_KEY, flatMetadata: boolean = false, ): Metadata { - const { metadata, embedding, ...rest } = node.toJSON(); + const { metadata, embedding, ...rest } = _.cloneDeep(node.toJSON()); if (flatMetadata) { validateIsFlat(metadata);