Skip to content

Commit

Permalink
Revert uuid usage (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer authored Jul 2, 2023
1 parent c68b846 commit 3164adb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-cherries-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

Fix regression with generated ids
12 changes: 9 additions & 3 deletions packages/core/react/use-chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useId, useRef, useState } from 'react'
import useSWRMutation from 'swr/mutation'
import useSWR, { KeyedMutator } from 'swr'
import { nanoid, createChunkDecoder } from '../shared/utils'
Expand All @@ -8,9 +8,14 @@ import type {
CreateMessage,
Message,
UseChatOptions,
RequestOptions,
ChatRequestOptions
} from '../shared/types'
import { ChatCompletionRequestMessageFunctionCall } from 'openai-edge'
import {
ChatCompletionRequestMessageFunctionCall,
CreateChatCompletionRequestFunctionCall
} from 'openai-edge'
import { ChatCompletionFunctions } from 'openai-edge/types/api'
export type { Message, CreateMessage, UseChatOptions }

export type UseChatHelpers = {
Expand Down Expand Up @@ -207,7 +212,8 @@ export function useChat({
body
}: UseChatOptions = {}): UseChatHelpers {
// Generate a unique id for the chat if not provided.
const chatId = id || `chat-${nanoid()}`
const hookId = useId()
const chatId = id || hookId

// 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: 5 additions & 3 deletions packages/core/react/use-completion.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useId, useRef, useState } from 'react'
import useSWRMutation from 'swr/mutation'
import useSWR from 'swr'

import { createChunkDecoder, nanoid } from '../shared/utils'
import { createChunkDecoder } from '../shared/utils'
import { UseCompletionOptions, RequestOptions } from '../shared/types'

export type UseCompletionHelpers = {
Expand Down Expand Up @@ -67,7 +67,9 @@ export function useCompletion({
onFinish,
onError
}: UseCompletionOptions = {}): UseCompletionHelpers {
const completionId = id || `completion-${nanoid()}`
// Generate an unique id for the completion if not provided.
const hookId = useId()
const completionId = id || hookId

// 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: 3 additions & 1 deletion packages/core/svelte/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export type UseChatHelpers = {
isLoading: Writable<boolean>
}

let uniqueId = 0

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

export function useChat({
Expand All @@ -66,7 +68,7 @@ export function useChat({
body
}: UseChatOptions = {}): UseChatHelpers {
// Generate a unique ID for the chat if not provided.
const chatId = id || `chat-${nanoid()}`
const chatId = id || `chat-${uniqueId++}`

const key = `${api}|${chatId}`
const { data, mutate: originalMutate } = useSWR<Message[]>(key, {
Expand Down
6 changes: 4 additions & 2 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, nanoid } from '../shared/utils'
import { createChunkDecoder } from '../shared/utils'

export type UseCompletionHelpers = {
/** The current completion result */
Expand Down Expand Up @@ -42,6 +42,8 @@ export type UseCompletionHelpers = {
isLoading: Writable<boolean>
}

let uniqueId = 0

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

export function useCompletion({
Expand All @@ -57,7 +59,7 @@ export function useCompletion({
onError
}: UseCompletionOptions = {}): UseCompletionHelpers {
// Generate an unique id for the completion if not provided.
const completionId = id || `completion-${nanoid()}`
const completionId = id || `completion-${uniqueId++}`

const key = `${api}|${completionId}`
const { data, mutate: originalMutate } = useSWR<string>(key, {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/vue/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ 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 @@ -67,7 +69,7 @@ export function useChat({
body
}: UseChatOptions = {}): UseChatHelpers {
// Generate a unique ID for the chat if not provided.
const chatId = id || `chat-${nanoid()}`
const chatId = id || `chat-${uniqueId++}`

const key = `${api}|${chatId}`
const { data, mutate: originalMutate } = useSWRV<Message[]>(
Expand Down
6 changes: 4 additions & 2 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, nanoid } from '../shared/utils'
import { createChunkDecoder } from '../shared/utils'

export type UseCompletionHelpers = {
/** The current completion result */
Expand Down Expand Up @@ -41,6 +41,8 @@ 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 @@ -58,7 +60,7 @@ export function useCompletion({
onError
}: UseCompletionOptions = {}): UseCompletionHelpers {
// Generate an unique id for the completion if not provided.
const completionId = id || `completion-${nanoid()}`
const completionId = id || `completion-${uniqueId++}`

const key = `${api}|${completionId}`
const { data, mutate: originalMutate } = useSWRV<string>(
Expand Down

0 comments on commit 3164adb

Please sign in to comment.