Skip to content

Commit

Permalink
Use rfc4122 uuids (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Leiter <max.leiter@vercel.com>
  • Loading branch information
0x5844 and MaxLeiter committed Jun 29, 2023
1 parent 46f9a2b commit fd82961
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-planes-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': major
---

Use rfc4122 IDs when generating chat/completion IDs
12 changes: 3 additions & 9 deletions packages/core/react/use-chat.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = {
Expand Down Expand Up @@ -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<Message[]>([api, chatId], null, {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/react/use-completion.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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<string>([api, completionId], null, {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/svelte/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export type UseChatHelpers = {
isLoading: Writable<boolean>
}

let uniqueId = 0

const store: Record<string, Message[] | undefined> = {}

export function useChat({
Expand All @@ -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<Message[]>(key, {
Expand Down
6 changes: 2 additions & 4 deletions packages/core/svelte/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -42,8 +42,6 @@ export type UseCompletionHelpers = {
isLoading: Writable<boolean>
}

let uniqueId = 0

const store: Record<string, any> = {}

export function useCompletion({
Expand All @@ -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<string>(key, {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/vue/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export type UseChatHelpers = {
isLoading: Ref<boolean>
}

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<string, Message[] | undefined> = {}
Expand All @@ -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<Message[]>(
Expand Down
6 changes: 2 additions & 4 deletions packages/core/vue/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -41,8 +41,6 @@ export type UseCompletionHelpers = {
isLoading: Ref<boolean>
}

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<string, any> = {}
Expand All @@ -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<string>(
Expand Down

0 comments on commit fd82961

Please sign in to comment.