File size: 809 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export const QWENPAW_CLIENT_MESSAGE_ID_KEY = "qwenpaw_client_message_id";

function randomBase36(length: number): string {
  const bytes = new Uint8Array(length);
  crypto.getRandomValues(bytes);
  let value = "";
  for (const byte of bytes) value += (byte % 36).toString(36);
  return value;
}

export function createClientMessageId(): string {
  return crypto.randomUUID?.() ?? `${Date.now()}-${randomBase36(16)}`;
}

export function attachClientMessageId(
  message: Record<string, unknown>,
  clientMessageId: string,
): Record<string, unknown> {
  const metadata =
    typeof message.metadata === "object" && message.metadata !== null
      ? message.metadata
      : {};
  return {
    ...message,
    metadata: {
      ...metadata,
      [QWENPAW_CLIENT_MESSAGE_ID_KEY]: clientMessageId,
    },
  };
}