From fd829612e9c4926c49dc2e207ba709d96d7a0794 Mon Sep 17 00:00:00 2001 From: Dann <85698684+sirlolcat@users.noreply.github.com> Date: Thu, 29 Jun 2023 23:20:15 +0000 Subject: [PATCH] Use rfc4122 uuids (#145) Co-authored-by: Max Leiter --- .changeset/young-planes-tan.md | 5 +++++ packages/core/react/use-chat.ts | 12 +++--------- packages/core/react/use-completion.ts | 8 +++----- packages/core/svelte/use-chat.ts | 4 +--- packages/core/svelte/use-completion.ts | 6 ++---- packages/core/vue/use-chat.ts | 4 +--- packages/core/vue/use-completion.ts | 6 ++---- 7 files changed, 17 insertions(+), 28 deletions(-) create mode 100644 .changeset/young-planes-tan.md diff --git a/.changeset/young-planes-tan.md b/.changeset/young-planes-tan.md new file mode 100644 index 00000000000..f71cc84da64 --- /dev/null +++ b/.changeset/young-planes-tan.md @@ -0,0 +1,5 @@ +--- +'ai': major +--- + +Use rfc4122 IDs when generating chat/completion IDs diff --git a/packages/core/react/use-chat.ts b/packages/core/react/use-chat.ts index 29d7085f9e3..5e0db495cf9 100644 --- a/packages/core/react/use-chat.ts +++ b/packages/core/react/use-chat.ts @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useId, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import useSWRMutation from 'swr/mutation' import useSWR, { KeyedMutator } from 'swr' import { nanoid, createChunkDecoder } from '../shared/utils' @@ -8,14 +8,9 @@ import type { CreateMessage, Message, UseChatOptions, - RequestOptions, ChatRequestOptions } from '../shared/types' -import { - ChatCompletionRequestMessageFunctionCall, - CreateChatCompletionRequestFunctionCall -} from 'openai-edge' -import { ChatCompletionFunctions } from 'openai-edge/types/api' +import { ChatCompletionRequestMessageFunctionCall } from 'openai-edge' export type { Message, CreateMessage, UseChatOptions } export type UseChatHelpers = { @@ -212,8 +207,7 @@ export function useChat({ body }: UseChatOptions = {}): UseChatHelpers { // Generate a unique id for the chat if not provided. - const hookId = useId() - const chatId = id || hookId + const chatId = id || `chat-${nanoid()}` // Store the chat state in SWR, using the chatId as the key to share states. const { data, mutate } = useSWR([api, chatId], null, { diff --git a/packages/core/react/use-completion.ts b/packages/core/react/use-completion.ts index 8a3336ea8e9..521dd39323f 100644 --- a/packages/core/react/use-completion.ts +++ b/packages/core/react/use-completion.ts @@ -1,8 +1,8 @@ -import { useCallback, useEffect, useId, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import useSWRMutation from 'swr/mutation' import useSWR from 'swr' -import { createChunkDecoder } from '../shared/utils' +import { createChunkDecoder, nanoid } from '../shared/utils' import { UseCompletionOptions, RequestOptions } from '../shared/types' export type UseCompletionHelpers = { @@ -67,9 +67,7 @@ export function useCompletion({ onFinish, onError }: UseCompletionOptions = {}): UseCompletionHelpers { - // Generate an unique id for the completion if not provided. - const hookId = useId() - const completionId = id || hookId + const completionId = id || `completion-${nanoid()}` // Store the completion state in SWR, using the completionId as the key to share states. const { data, mutate } = useSWR([api, completionId], null, { diff --git a/packages/core/svelte/use-chat.ts b/packages/core/svelte/use-chat.ts index 8d8600144e7..51d94c69cec 100644 --- a/packages/core/svelte/use-chat.ts +++ b/packages/core/svelte/use-chat.ts @@ -50,8 +50,6 @@ export type UseChatHelpers = { isLoading: Writable } -let uniqueId = 0 - const store: Record = {} export function useChat({ @@ -68,7 +66,7 @@ export function useChat({ body }: UseChatOptions = {}): UseChatHelpers { // Generate a unique ID for the chat if not provided. - const chatId = id || `chat-${uniqueId++}` + const chatId = id || `chat-${nanoid()}` const key = `${api}|${chatId}` const { data, mutate: originalMutate } = useSWR(key, { diff --git a/packages/core/svelte/use-completion.ts b/packages/core/svelte/use-completion.ts index f5840584816..90ea7be402a 100644 --- a/packages/core/svelte/use-completion.ts +++ b/packages/core/svelte/use-completion.ts @@ -4,7 +4,7 @@ import { Readable, get, writable } from 'svelte/store' import { Writable } from 'svelte/store' import type { UseCompletionOptions, RequestOptions } from '../shared/types' -import { createChunkDecoder } from '../shared/utils' +import { createChunkDecoder, nanoid } from '../shared/utils' export type UseCompletionHelpers = { /** The current completion result */ @@ -42,8 +42,6 @@ export type UseCompletionHelpers = { isLoading: Writable } -let uniqueId = 0 - const store: Record = {} export function useCompletion({ @@ -59,7 +57,7 @@ export function useCompletion({ onError }: UseCompletionOptions = {}): UseCompletionHelpers { // Generate an unique id for the completion if not provided. - const completionId = id || `completion-${uniqueId++}` + const completionId = id || `completion-${nanoid()}` const key = `${api}|${completionId}` const { data, mutate: originalMutate } = useSWR(key, { diff --git a/packages/core/vue/use-chat.ts b/packages/core/vue/use-chat.ts index f6818253c04..0bfd886508d 100644 --- a/packages/core/vue/use-chat.ts +++ b/packages/core/vue/use-chat.ts @@ -49,8 +49,6 @@ export type UseChatHelpers = { isLoading: Ref } -let uniqueId = 0 - // @ts-expect-error - some issues with the default export of useSWRV const useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv const store: Record = {} @@ -69,7 +67,7 @@ export function useChat({ body }: UseChatOptions = {}): UseChatHelpers { // Generate a unique ID for the chat if not provided. - const chatId = id || `chat-${uniqueId++}` + const chatId = id || `chat-${nanoid()}` const key = `${api}|${chatId}` const { data, mutate: originalMutate } = useSWRV( diff --git a/packages/core/vue/use-completion.ts b/packages/core/vue/use-completion.ts index 1d49c1f5c3a..b0e67d66752 100644 --- a/packages/core/vue/use-completion.ts +++ b/packages/core/vue/use-completion.ts @@ -3,7 +3,7 @@ import { ref } from 'vue' import type { Ref } from 'vue' import type { UseCompletionOptions, RequestOptions } from '../shared/types' -import { createChunkDecoder } from '../shared/utils' +import { createChunkDecoder, nanoid } from '../shared/utils' export type UseCompletionHelpers = { /** The current completion result */ @@ -41,8 +41,6 @@ export type UseCompletionHelpers = { isLoading: Ref } -let uniqueId = 0 - // @ts-expect-error - some issues with the default export of useSWRV const useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv const store: Record = {} @@ -60,7 +58,7 @@ export function useCompletion({ onError }: UseCompletionOptions = {}): UseCompletionHelpers { // Generate an unique id for the completion if not provided. - const completionId = id || `completion-${uniqueId++}` + const completionId = id || `completion-${nanoid()}` const key = `${api}|${completionId}` const { data, mutate: originalMutate } = useSWRV(