generateUUID() util in vercel/ai-chatbot source code.

Ramu NarasingaRamu Narasinga
3 min read

In this article, we will review a function, generateUUID, found in vercel/ai-chatbot source code but first, let’s find out where this function is called from.

In the file, create-document.ts, you will find the below import at line #1

import { generateUUID } from '@/lib/utils';

and then at line #24, you will this function being called inside the execute function

 execute: async ({ title, kind }) => {
      const id = generateUUID();

Okay, now that we saw how this function is called, let’s learn its definition.

At line #48, in lib/util.ts, You will find the below code:

export function generateUUID(): string {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    const v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

This function is obviously used to generate random id but the algorithm used here is something I haven’t seen before.

'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c)

It uses .replace(/[xy]/g, ...) to find every x or y in the string and replace it with a computed value. — Source: ChatGPT

const r = (Math.random() * 16) | 0;

This is a quick and clever way to generate a random integer between 0 and 15 (inclusive). — Source: ChatGPT

const v = c === 'x' ? r : (r & 0x3) | 0x8;

If the character is 'x':

v = r;

Just use the random value r (between 0–15).

No constraints — we just need a hex digit

If the character is 'y':

v = (r & 0x3) | 0x8;

This is bit-level wizardry to force the y digit to follow the UUID variant rules, which say:

The first two bits of the y character should be 10 in binary.

So, here’s how the logic achieves that:

Step-by-step:

r & 0x3: keeps only the last 2 bits of r
→ this gives values between 0 and 3 (00, 01, 10, 11)

| 0x8: sets the first two bits to 10 (i.e., 1000 in binary → 8 in hex)

That guarantees v will be one of:

8 (1000), 9 (1001), a (1010), or b (1011)

— Source: ChatGPT

I highlighted the answers from ChatGPT here in this article, but from what I understood, that “y” in the string exists for a reason and that reason is that UUID follows a guideline that expects “y” to follow a certain pattern.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Github — https://github.com/ramu-narasinga

My website — https://ramunarasinga.com

My Youtube channel — https://www.youtube.com/@ramu-narasinga

Learning platform — https://thinkthroo.com

Codebase Architecture — https://app.thinkthroo.com/architecture

Best practices — https://app.thinkthroo.com/best-practices

Production-grade projects — https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/vercel/ai-chatbot/blob/main/lib/ai/tools/create-document.ts#L1

  2. https://github.com/vercel/ai-chatbot/blob/main/lib/ai/tools/create-document.ts#L24

  3. https://github.com/vercel/ai-chatbot/blob/main/lib/utils.ts#L48

  4. https://github.com/uuidjs/uuid/tree/main/src

0
Subscribe to my newsletter

Read articles from Ramu Narasinga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ramu Narasinga
Ramu Narasinga

I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.