From 1e6171521b098fd09f44deaae8d81e6efd63b061 Mon Sep 17 00:00:00 2001
From: Alex Yang <himself65@outlook.com>
Date: Fri, 12 Jan 2024 12:30:17 -0600
Subject: [PATCH] refactor: use `crypto.randomUUID` (#371)

Ref: https://nodejs.org/docs/latest/api/crypto.html#cryptorandomuuidoptions
---
 packages/core/package.json             |  2 --
 packages/core/src/ChatEngine.ts        |  8 ++++----
 packages/core/src/GlobalsHelper.ts     |  4 ++--
 packages/core/src/Node.ts              |  7 +++----
 packages/core/src/QueryEngine.ts       |  8 ++++----
 packages/core/src/indices/BaseIndex.ts |  4 ++--
 pnpm-lock.yaml                         | 15 ---------------
 7 files changed, 15 insertions(+), 33 deletions(-)

diff --git a/packages/core/package.json b/packages/core/package.json
index 011f650a2..b51548236 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -27,7 +27,6 @@
     "rake-modified": "^1.0.8",
     "replicate": "^0.21.1",
     "string-strip-html": "^13.4.3",
-    "uuid": "^9.0.1",
     "wink-nlp": "^1.14.3"
   },
   "devDependencies": {
@@ -36,7 +35,6 @@
     "@types/node": "^18.19.2",
     "@types/papaparse": "^5.3.14",
     "@types/pg": "^8.10.9",
-    "@types/uuid": "^9.0.7",
     "node-stdlib-browser": "^1.2.0",
     "tsup": "^7.2.0",
     "typescript": "^5.3.2"
diff --git a/packages/core/src/ChatEngine.ts b/packages/core/src/ChatEngine.ts
index 1afbb13d2..b1df2aed5 100644
--- a/packages/core/src/ChatEngine.ts
+++ b/packages/core/src/ChatEngine.ts
@@ -1,4 +1,4 @@
-import { v4 as uuidv4 } from "uuid";
+import { randomUUID } from "node:crypto";
 import { ChatHistory } from "./ChatHistory";
 import { NodeWithScore, TextNode } from "./Node";
 import {
@@ -206,7 +206,7 @@ export class DefaultContextGenerator implements ContextGenerator {
   async generate(message: string, parentEvent?: Event): Promise<Context> {
     if (!parentEvent) {
       parentEvent = {
-        id: uuidv4(),
+        id: randomUUID(),
         type: "wrapper",
         tags: ["final"],
       };
@@ -272,7 +272,7 @@ export class ContextChatEngine implements ChatEngine {
     }
 
     const parentEvent: Event = {
-      id: uuidv4(),
+      id: randomUUID(),
       type: "wrapper",
       tags: ["final"],
     };
@@ -304,7 +304,7 @@ export class ContextChatEngine implements ChatEngine {
     chatHistory = chatHistory ?? this.chatHistory;
 
     const parentEvent: Event = {
-      id: uuidv4(),
+      id: randomUUID(),
       type: "wrapper",
       tags: ["final"],
     };
diff --git a/packages/core/src/GlobalsHelper.ts b/packages/core/src/GlobalsHelper.ts
index d1d82280d..d9d4495b5 100644
--- a/packages/core/src/GlobalsHelper.ts
+++ b/packages/core/src/GlobalsHelper.ts
@@ -1,6 +1,6 @@
 import { encodingForModel } from "js-tiktoken";
 
-import { v4 as uuidv4 } from "uuid";
+import { randomUUID } from "node:crypto";
 import { Event, EventTag, EventType } from "./callbacks/CallbackManager";
 
 export enum Tokenizers {
@@ -64,7 +64,7 @@ class GlobalsHelper {
     tags?: EventTag[];
   }): Event {
     return {
-      id: uuidv4(),
+      id: randomUUID(),
       type,
       // inherit parent tags if tags not set
       tags: tags || parentEvent?.tags,
diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts
index fef42df93..a4a2dcfad 100644
--- a/packages/core/src/Node.ts
+++ b/packages/core/src/Node.ts
@@ -1,7 +1,6 @@
 import _ from "lodash";
-import { createHash } from "node:crypto";
-import path from "path";
-import { v4 as uuidv4 } from "uuid";
+import { createHash, randomUUID } from "node:crypto";
+import path from "node:path";
 
 export enum NodeRelationship {
   SOURCE = "SOURCE",
@@ -49,7 +48,7 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
    *
    * Set to a UUID by default.
    */
-  id_: string = uuidv4();
+  id_: string = randomUUID();
   embedding?: number[];
 
   // Metadata fields
diff --git a/packages/core/src/QueryEngine.ts b/packages/core/src/QueryEngine.ts
index aedbb9be7..6c4fa3d28 100644
--- a/packages/core/src/QueryEngine.ts
+++ b/packages/core/src/QueryEngine.ts
@@ -1,4 +1,4 @@
-import { v4 as uuidv4 } from "uuid";
+import { randomUUID } from "node:crypto";
 import { NodeWithScore, TextNode } from "./Node";
 import {
   BaseQuestionGenerator,
@@ -72,7 +72,7 @@ export class RetrieverQueryEngine implements BaseQueryEngine {
 
   async query(query: string, parentEvent?: Event) {
     const _parentEvent: Event = parentEvent || {
-      id: uuidv4(),
+      id: randomUUID(),
       type: "wrapper",
       tags: ["final"],
     };
@@ -136,14 +136,14 @@ export class SubQuestionQueryEngine implements BaseQueryEngine {
 
     // groups final retrieval+synthesis operation
     const parentEvent: Event = {
-      id: uuidv4(),
+      id: randomUUID(),
       type: "wrapper",
       tags: ["final"],
     };
 
     // groups all sub-queries
     const subQueryParentEvent: Event = {
-      id: uuidv4(),
+      id: randomUUID(),
       parentId: parentEvent.id,
       type: "wrapper",
       tags: ["intermediate"],
diff --git a/packages/core/src/indices/BaseIndex.ts b/packages/core/src/indices/BaseIndex.ts
index 3d44799a7..98e60ac5d 100644
--- a/packages/core/src/indices/BaseIndex.ts
+++ b/packages/core/src/indices/BaseIndex.ts
@@ -1,4 +1,4 @@
-import { v4 as uuidv4 } from "uuid";
+import { randomUUID } from "node:crypto";
 import { BaseNode, Document, jsonToNode } from "../Node";
 import { BaseQueryEngine } from "../QueryEngine";
 import { BaseRetriever } from "../Retriever";
@@ -16,7 +16,7 @@ export abstract class IndexStruct {
   indexId: string;
   summary?: string;
 
-  constructor(indexId = uuidv4(), summary = undefined) {
+  constructor(indexId = randomUUID(), summary = undefined) {
     this.indexId = indexId;
     this.summary = summary;
   }
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 214a3896d..6bdc83178 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -212,9 +212,6 @@ importers:
       string-strip-html:
         specifier: ^13.4.3
         version: 13.4.3
-      uuid:
-        specifier: ^9.0.1
-        version: 9.0.1
       wink-nlp:
         specifier: ^1.14.3
         version: 1.14.3
@@ -234,9 +231,6 @@ importers:
       '@types/pg':
         specifier: ^8.10.9
         version: 8.10.9
-      '@types/uuid':
-        specifier: ^9.0.7
-        version: 9.0.7
       node-stdlib-browser:
         specifier: ^1.2.0
         version: 1.2.0
@@ -4794,10 +4788,6 @@ packages:
     resolution: {integrity: sha512-d0XxK3YTObnWVp6rZuev3c49+j4Lo8g4L1ZRm9z5L0xpoZycUPshHgczK5gsUMaZOstjVYYi09p5gYvUtfChYw==}
     dev: false
 
-  /@types/uuid@9.0.7:
-    resolution: {integrity: sha512-WUtIVRUZ9i5dYXefDEAI7sh9/O7jGvHg7Df/5O/gtH3Yabe5odI3UWopVR1qbPXQtvOxWu3mM4XxlYeZtMWF4g==}
-    dev: true
-
   /@types/validate-npm-package-name@3.0.0:
     resolution: {integrity: sha512-iFNNIrEaJH1lbPiyX+O/QyxSbKxrTjdNBVZGckt+iEL9So0hdZNBL68sOfHnt2txuUD8UJXvmKv/1DkgkebgUg==}
     dev: true
@@ -16047,11 +16037,6 @@ packages:
     hasBin: true
     dev: false
 
-  /uuid@9.0.1:
-    resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==}
-    hasBin: true
-    dev: false
-
   /v8-compile-cache-lib@3.0.1:
     resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==}
     dev: true
-- 
GitLab