Dax Raad 9 months ago
parent
commit
bcd2fd68b7
3 changed files with 36 additions and 10 deletions
  1. 32 6
      js/src/id/id.ts
  2. 1 1
      js/src/index.ts
  3. 3 3
      js/src/session/session.ts

+ 32 - 6
js/src/id/id.ts

@@ -1,5 +1,5 @@
-import { ulid } from "ulid";
 import { z } from "zod";
+import { randomBytes } from "crypto";
 
 export namespace Identifier {
   const prefixes = {
@@ -7,18 +7,44 @@ export namespace Identifier {
     message: "msg",
   } as const;
 
-  export function create(
+  export function schema(prefix: keyof typeof prefixes) {
+    return z.string().startsWith(prefixes[prefix]);
+  }
+
+  const LENGTH = 24;
+
+  export function ascending(prefix: keyof typeof prefixes, given?: string) {
+    return generateID(prefix, false, given);
+  }
+
+  export function descending(prefix: keyof typeof prefixes, given?: string) {
+    return generateID(prefix, true, given);
+  }
+
+  function generateID(
     prefix: keyof typeof prefixes,
+    descending: boolean,
     given?: string,
   ): string {
     if (given) {
       if (given.startsWith(prefixes[prefix])) return given;
       throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
     }
-    return [prefixes[prefix], ulid()].join("_");
-  }
 
-  export function schema(prefix: keyof typeof prefixes) {
-    return z.string().startsWith(prefixes[prefix]);
+    let now = BigInt(Date.now());
+
+    if (descending) {
+      now = ~now;
+    }
+
+    const timeBytes = Buffer.alloc(6);
+    for (let i = 0; i < 6; i++) {
+      timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff));
+    }
+
+    const randLength = (LENGTH - 12) / 2;
+    const random = randomBytes(randLength);
+
+    return prefix + "_" + timeBytes.toString("hex") + random.toString("hex");
   }
 }

+ 1 - 1
js/src/index.ts

@@ -16,7 +16,7 @@ App.provide(app, async () => {
 
   await Session.chat(sessionID, {
     role: "user",
-    id: Identifier.create("message"),
+    id: Identifier.ascending("message"),
     parts: [
       {
         type: "text",

+ 3 - 3
js/src/session/session.ts

@@ -34,7 +34,7 @@ export namespace Session {
 
   export async function create() {
     const result: Info = {
-      id: Identifier.create("session"),
+      id: Identifier.descending("session"),
       title: "New Session - " + new Date().toISOString(),
     };
     log.info("created", result);
@@ -86,7 +86,7 @@ export namespace Session {
     l.info("chatting");
     const msgs = (await messages(sessionID)) ?? [
       {
-        id: Identifier.create("message"),
+        id: Identifier.ascending("message"),
         role: "system",
         parts: [
           {
@@ -125,7 +125,7 @@ export namespace Session {
       model,
     });
     const next: UIMessage = {
-      id: Identifier.create("message"),
+      id: Identifier.ascending("message"),
       role: "assistant",
       parts: [],
     };